Angular 报错:Error: StaticInjectorError(e)[t -> t]: StaticInjectorError(Platform: core)[t -> t]: NullInjectorError: No provider for t!

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

希望对大家有所帮助!

猜你喜欢

转载自www.cnblogs.com/nanmu0/p/12535097.html
今日推荐