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 { }