dependency-injection - 角 5 : Can't resolve all parameters for component

标签 dependency-injection angular5

我在教程中遇到了以 Angular 5 注入(inject)服务的问题。

我有一个简单的服务

@Injectable()
export class SimpleService {
    ...
}

还有一个简单的组件:
@Component({...})
export class SimpleComponent {
    constructor(private simpleService SimpleService) {}
}

服务设置为 providers在模块中:
@NgModule({
    ...
    providers: [SimpleService]
    ...
})
export class SimpleModule {}

我在控制台中看到以下错误:
Error: Can't resolve all parameters for SimpleComponent

但是,如果我注入(inject) SimpleService@Inject
@Inject(SimpleService) private simpleService: SimpleService

错误消失。

我究竟做错了什么?

更新:
我看到几个答案,人们建议添加 emitDecoratorMetadata: true到 tsconfig 文件。但是这条线已经存在了

最佳答案

我遇到了类似的问题,这可能对其他人有所帮助,因为这是我在搜索时找到的最接近的匹配项。

我的构造函数中有一个错误。我忘了声明服务的类型:

我曾写过:

constructor(
    private myService
){}

代替:
constructor(
    private myService: MyService
){}

关于dependency-injection - 角 5 : Can't resolve all parameters for component,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50880783/

相关文章:

Angular 5 表单验证(必需)不起作用

c# - 使用 IOption 的类的实例化

dependency-injection - 简单注入(inject)器 webapi 授权属性

dependency-injection - 是否存在初始化通过 DI 容器创建的对象的模式

angular - 如何指定从根目录到 Assets Angular 的路径?

angular - 如何使用 ngrx 的 StoreModule.forRoot() 和 StoreModule.forFeature() 访问另一个模块存储

javascript - 如何使用 overflow-y 滚动到 div 中的元素

javascript - 使用 Observable.toPromise() 处理全局错误

c# - 如何/在何处使用 PRISM 和 Unity 在 MVVM 中实例化模型对象

dependency-injection - 如何在 NestJS 中使用文字类型作为服务构造函数参数