我可以这样做:角2:房产注入,而不是构造函数注入
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',
templateUrl: 'templates.component.html',
styleUrls: [ 'templates.component.scss' ],
encapsulation: ViewEncapsulation.None
})
export class SportTemplates extends BaseComponent implements OnInit {
constructor(appConfig: AppConfig) {
super(appConfig);
}
你有没有发现用于构造器注入的最佳解决方案? – PaladiN
我在这里找到了我的问题的答案: https://stackoverflow.com/questions/42461852/angular-2-inject-service-manually谢谢。 –