1
我想知道为什么Angular 2提供程序语法在我想通过useValue
字段提供特定值时不起作用。Angular 2提供程序不能使用useValue和函数调用新的
例如,在app.module.ts我
// Abstracted base class (since interfaces don't work with libraries and tsc)
export class Base {
constructor(public bar:Bar) {
}
}
export class Foo extends Base {
constructor(public bar:Bar) {
super(bar);
}
}
export class Bar {
hello = "world";
}
@NgModule({
declarations: [
AppComponent,
],
imports: [
],
providers: [
{provide: Base, useValue: new Foo(new Bar())}
],
bootstrap: [AppComponent]
})
export class AppModule {
}
尝试,这将引发期间ng build
一个错误:我简单地使用领域得到解决此
ERROR in Error encountered resolving symbol values statically. Calling function 'Foo', function calls are not supported. Consider replacing the function or lambda with a reference to an exported function, resolving symbol AppModule in /Users/tbeauvais/Code/example-sdk/src/app/app.module.ts, resolving symbol AppModule in /Users/tbeauvais/Code/example-sdk/src/app/app.module.ts
,useClass
,然后通过如下方式指定参数deps
:
...
providers: [
Bar,
{provide: Base, useClass: Foo, deps:[Bar]}
],
...
它的作品,它只是不理想,我想知道我在这里失踪。另外,我不想无缘无故地注入Bar
。
任何帮助将不胜感激。干杯!
你应该检查:https://github.com/rangle/angular-2-aot-sandbox – echonax