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