I'm assuming your class represents a service.
You can define a base class where you set the shared logic for your service and then extend that class with different implementations of the b()
method. To avoid duplication of your b()
logic, you can encapsulate that logic in a protected method on base class and let your public and private version of b()
just call that method:
export class BaseService {
protected _b() {
return 'This string comes from b() method'
}
c() {
return 'This string comes from 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 provide BaseServiceWithPublicB
in module M, 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 { }