[英]Angular2.3 Custom Library and dependency injection
我試圖將使用yeoman創建的自定義庫中的服務注入到我現有的ionic2項目中。 lib的index.ts(將作為npm模塊安裝):
@NgModule({
imports: [
CommonModule
],
declarations: [
SampleComponent,
SampleDirective,
],
exports: [
SampleComponent,
SampleDirective,
SamplePipe
]
})
export default class SampleModule {
static forRoot(): ModuleWithProviders {
return {
ngModule: SampleModule,
providers: [SampleService, SettingsSVC]
};
}
}
在我的ionic2應用程序中,我想注入SettingsSVC和module.ts。 如果我將其添加到app.module的imports-Section中,它說:
模塊“ AppModule”導入的異常值“ SettingsSVC”
如果沒有,我會收到一個控制台錯誤“找不到提供程序”。
可以識別並鏈接類型(無論@Injectable
為何的類)本身,並且如果我將同一個類添加到模塊中的Ionic2 App及其提供程序部分,則它將與Injection一起使用。
關於如何使其工作的任何建議?
嘗試這個,
@NgModule({
imports: [SampleModule.forRoot()],
declaretions: [],
providers: []
})
export class YourAppModule {}
如果顯示意外值,通常表示您導入的內容均未成功導入。
要么:
1- SettingsSVC的路徑錯誤
2-SettingsSVC的名稱錯誤
3-路徑正確,但未導出SettingsSVC
因此,要確保導入實際上是導入的SettingsSVC,請在其后放置console.log並注銷
import {SettingsSVC} from 'the/path/'
console.log('SettingsSVC',SettingsSVC);
如果日志中顯示undefined,則肯定是上述之一(或類似的東西)。
順便說一句,我有些困惑,您是否要將SettingsSVC作為服務導入並放入您的提供程序中,還是想將其作為模塊導入並放入導入中:[]?
不能同時擁有!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.