[英]Angular 5 Unit Test: “Can't read property 'get' of undefined in Injector”
[英]Can't get injector in angular 2
我有DrawingDataService
,其中包含我的數據數組和不同的工具來繪制此數據。 我想在所有工具中使用DrawingDataService
類的DrawingDataService
。
我在AppModule提供程序列表中設置DrawingDataService
(使其成為單例實例):
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule
],
providers: [
DrawingDataService
],
bootstrap: [AppComponent]
})
export class AppModule { }
然后在我的工具中添加注射器:
export class LineTool extends BaseTool {
constructor(private injector: Injector) {
super();
let drawingDataService2 = this.injector.get(DrawingDataService);
它告訴我this.injector
為null。 進行單例服務以存儲數據是否是一個好習慣?
嘗試以以下方式執行此操作:
constuctor( private drawingDataService: DrawingDataService)
您可以通過以下方式使用它:
this.drawingDataService.someMethod()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.