Angular使用中報錯:Error: StaticInjectorError(e)[t -> t]: StaticInjectorError(Platform: core)[t -> t]: NullInjectorError: No provider for t!
報錯原因:我在 detail.componnet.ts 中調用了另一個組件 redisListComponent 中的 changeInstanceStatus 方法,如下圖:
雖然我在 detail.componnet.ts 已經 import 了 redisListComponent 組件,並且在 constructor 中加入了 private redisListComponent: RedisListComponent。但是任然會報錯
上網查詢后找到解決方法: 轉載自:https://www.hellojava.com/a/69235.html
方法1:不光要 import,還需要在 @Component 中加入 providers: [RedisListComponent],這樣才能將你當前的組件與所需組件相關聯。如下圖所示:
方法2:此方法是將你希望關聯的組件作為全局的:在 app.modules 中的 @NgModule({})中加入 providers: [RedisListComponent]
@NgModule({
declarations: [...],
imports: [...],
bootstrap: [...],
entryComponents: [...],
providers: [
RedisListComponent
]
})
以上方法轉載自:https://www.hellojava.com/a/69235.html
希望對大家有所幫助!