![](/img/trans.png)
[英]angular 9 dependency injection error. " The class cannot be created via dependency injection, as it does not have an Angular decorator
[英]Angular 9 : The class cannot be created via dependency injection, as it does not have an Angular decorator
这个错误把我逼到了墙角。 我有一个 authguard 服务,如此处所示。 无论我做什么,编译器都会一直给我这个错误
'
Either add the @Injectable() decorator to 'AuthGuard', or configure a different provider (such as a provider with 'useFactory').
'
@Injectable({ providedIn: 'root' })
export class AuthGuard implements CanActivate {
constructor(private myService: MyService,
private router: Router)}
我正在我的 app.module 中实现它
providers: [
AuthGuard
]
bootstrap: [AppComponent]
})
export class AppModule { }
我显然在 Authguard class 上方有可注入装饰器,但我仍然收到此错误。 这只发生在我从 Angular 8 升级到 9.1 时,这是 VS Code 或编译器的某种错误还是什么?
您正在从 2 个地方注册相同的依赖项
首先是@Injectable({ providedIn: 'root' })
。 您正在使用此语法{ providedIn: 'root' }
根注入器注册。
其次是在您再次注册的下方,这就是您出错的原因。
providers: [
AuthGuard
]
只需将其从提供程序数组中删除,它就会解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.