Can a class method be utilized within a Module without being shared with other modules in Angular and TypeScript?

Here is a scenario to consider

  export class X{
    y():void{}
    z():void{}
    }

I am currently in Module N I need to utilize method y() and z() within module N, but I want to restrict access to method y() from other modules while still allowing access to method z(). Is this achievable in Angular?

Answer №1

If your class represents a service, it's best to follow a structured approach.

You can create a base class that contains the common functionality for your service and then extend this class to implement different versions of the b() method. To prevent repeating the logic in the b() method, you can encapsulate it within a protected method in the base class and have both the public and private variations of b() call this method:

export class BaseService {
    protected _b() {
        return 'This string is from the b() method'
    }
    
    c() {
        return 'This string is from the c() method'
    }
}

@Injectable()
export class BaseServiceWithPublicB extends BaseService {
    b() {
        return super._b();
    }
}

@Injectable()
export class BaseServiceWithPrivateB extends BaseService {
    private b() {
        return super._b();
    }
}

Then you can provide BaseServiceWithPublicB in module M, and BaseServiceWithPrivateB in any other module:

@NgModule({
    declarations: [],
    providers: [{provide: BaseService, useClass: BaseServiceWithPublicB}],
    exports: []
})
export class ModuleWithPublicB { }

@NgModule({
    declarations: [],
    providers: [{provide: BaseService, useClass: BaseServiceWithPrivateB}],
    exports: []
})
export class ModuleWithPrivateB { }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Fill a dynamic form with a date sourced from the ngrx storage

How can I populate a form with data from the store if setValue and patchValue methods are not working? export class MyComponent implements OnInit, OnDestroy { public newsletterToEdit$: Observable<NewNewsletter> = this.store.selectNewsletterToEdi ...

Unable to utilize the concatMap() method in conjunction with the angular2 Http feature

Looking to chain multiple http requests, the number of requests can vary and they are not dependent on the previous request's result. The goal is to only keep the returned object from the last request. Received two suggestions on this forum thread. T ...

Guide to setting up value observation in React Context for optimal functionality

Imagine a scenario where there is a Parent Component that provides a Context containing a Store Object. This Store holds a value and a function to update this value. class Store { // value // function updateValue() {} } const Parent = () => { const ...

Issue with ngModel value not being accurately represented by checkbox state in Angular 2

My issue lies with a checkbox that does not reflect its ngModel value. To provide some context, I have a Service managing a list of products and a component responsible for displaying this list and allowing users to select or deselect products. If a user d ...

Navigating Angular QueryList through loops

I am currently trying to gather all the images in my component and store them in an array. To achieve this, I am utilizing Angular's @ViewChildren which returns a QueryList of ElementRef: @ViewChildren('img', { read: ElementRef }) images: Q ...

An issue arose during the installation of the package 'npm i angularfire2'

I am currently working on an Angular project and I am trying to import AngularFireStorage using the following line of code: import { AngularFireStorage } from 'angularfire2/storage'; I attempted to do this by running the command npm i angularfire ...

The Angular Library integrates parent application modules into its structure

Is it possible for an Angular application to inherit a service from the core application and utilize its functions? In my core application, I have several libraries and I require all of them to be able to utilize a specific service from within the core ap ...

Angular 2 variable reference

Within my appComponent, I have the line this.loggedIn = this.authenticationService.isLogged;. This means that appComponent is utilizing authenticationService to retrieve the isLogged data. I assume that this.loggedIn is referencing the data from the servi ...

Properly implement Angular/Typescript to populate an array with chosen objects

Currently, I have an Angular application that is fetching JSON resources from a Spring Boot REST API. These resources consist of simple player objects with attributes like id, name, position, and value. On the UI, each object is displayed along with a "BUY ...

Transfer dynamically generated table data to the following page

Seeking guidance on a common issue I'm facing. I am creating a table using data from Firebase upon page load, and I want users to click on a row to view specific details of that item. It may sound confusing, but it will make more sense with the code p ...

Exploring the World of Popper.js Modifiers

Within our React and Typescript application, we integrate the react-datepicker library, which utilizes popper.js. Attempting to configure PopperModifiers according to the example provided here: . Despite replicating the exact setup from the example, a typ ...

Manipulate form components within ngAfterViewInit

I am looking for a way to programmatically access form controls and disable specific controls based on certain conditions. The issue I am facing is that the form in my code snippet does not contain any controls: Component export class OfferDialogComponen ...

Running headless Chrome with Protractor on Windows platform is presenting difficulties

While there is a wealth of documentation available on headless chrome automated testing, information specifically for Windows users seems to be lacking. Furthermore, details on utilizing headless chrome for end-to-end automated testing in a fully develope ...

What is preventing me from adjusting the padding of the mat-button?

Trying to adjust the default padding of a mat-button, but encountering issues with changing the component's style. Any suggestions on how to subscribe to the default padding (16px)? I've attempted modifying CSS properties to set the padding of a ...

Customizing the appearance of the Material UI MuiClockPicker with unique style

I am wondering how I can override the styles for MuiClockPicker? I discovered that using createTheme to override the styles actually works for me, but I encountered an error from TypeScript: TS2322: Type '{ MuiOutlinedInput: { styleOverrides: { roo ...

There was a parsing error due to encountering an unexpected reserved word 'interface' in the code, as flagged

I'm encountering an issue with my code when trying to utilize Props. The error message I'm receiving is "Parsing error: Unexpected reserved word 'interface'. (3:0)eslint". This project is being developed using next with TypeScript. Er ...

Deciphering the Mysteries of API Gateway Caching

It seems like a common pattern to enable an API Gateway to serve an Angular Webapp by pulling it from S3. The setup involves having the API gateway with a GET request set up at the / route to pull index.html from the appropriate location in the S3 bucket, ...

Troubles with applying Global Themes in StyledComponents for React Native

Problem with Global Theme in StyledComponents (React Native) When attempting to utilize a color from my global theme in my component and setting it like so: background-color: ${({theme}) => theme.} The properties within theme, such as colors, font-siz ...

During the installation of npm in my angular project directory, an error occurred

Encountered an error while installing packages (npm)...npm ERR! code ERR_SOCKET_TIMEOUT npm ERR! errno ERR_SOCKET_TIMEOUT npm ERR! network Received an invalid response when trying to fetch https://registry.npmjs.org/@babel%2fplugin-proposal-nullish-coalesc ...

What is the best way to verify the type of an object received from request.body in Typescript

Is it possible to check the object type from the request body and then execute the appropriate function based on this type? I have attempted to do so in the following manner: export interface SomeBodyType { id: string, name: string, [etc....] } ...