我可以这样做:
export class BaseComponent { protected config: IConfig; @Inject(AppConfig) protected appConfig: AppConfig; constructor() { this.config = this.appConfig.getConfig(); }
而不是这个:
export class BaseComponent { config: IConfig; constructor( private appConfig: AppConfig,) { this.config = appConfig.getConfig(); }
目标是简化构造函数签名,因此所有子组件都不需要在其构造函数中指定appConfig.所以从BaseComponent继承的组件看起来像这样:
@Component({ selector: 'sport-templates',templateUrl: 'templates.component.html',styleUrls: [ 'templates.component.scss' ],encapsulation: ViewEncapsulation.None }) export class SportTemplates extends BaseComponent implements OnInit { constructor() { super(); }
而是这样:
@Component({ selector: 'sport-templates',encapsulation: ViewEncapsulation.None }) export class SportTemplates extends BaseComponent implements OnInit { constructor(appConfig: AppConfig) { super(appConfig); }
你可以这样做:
原文链接:https://www.f2er.com/angularjs/141711.htmlmyService: MyService = this.injector.get(MyService); constructor(private injector:Injector) {}
注射器位于@ angular / core中