简体   繁体   English

Angular 5:无法进行依赖注入

[英]Angular 5: cannot get dependency injection working

I have a angular 5 ("@angular/core": "5.1.2") app and I keep getting this error 我有一个角度5(“ @ angular / core”:“ 5.1.2”)应用,但我不断收到此错误

Uncaught Error: Can't resolve all parameters for FooComponent: (?). 未捕获的错误:无法解析FooComponent的所有参数:(?)。

Service: 服务:

@Injectable()
export class FooService { }

Component: 零件:

@Component({template: '<p></p>'})
export class FooComponent {
    constructor(private s: FooService) { }
}

Module: 模块:

@NgModule({
    declarations: [ AppComponent, FooComponent ],
    bootstrap:[ AppComponent ],
    providers: [ FooService ] })
export class AppModule {}

My tsconfig: 我的tsconfig:

{
    "compilerOptions": {
        "baseUrl": "",
        "declaration": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "noImplicitAny": false,
        "lib": [
            "es6",
            "dom"
        ],
        "mapRoot": "./",
        "module": "commonjs",
        "moduleResolution": "node",
        "rootDir": "./app",
        "outDir": "../dist",
        "sourceMap": true,
        "target": "es5",
        "noEmitOnError": true,
        "typeRoots": [
          "./node_modules/@types"
        ]
    },
    "include": [
      "./**/*.ts"
    ]
}

Any idea what I am missing? 知道我缺少什么吗? Note that I am using es6 lib and @Injectable doesn't take any parameters. 请注意,我使用的是es6 lib, @Injectable没有任何参数。 I can't seem to find many good examples. 我似乎找不到很多好的例子。

Generally how do we debug DI on angular? 通常我们如何在角度上调试DI?

@Component({
    template: '<p></p>'
})
export class FooComponent {
    constructor(@Inject("FooService") private s: FooService) { }
}

Missed @Inject 错过了@Inject

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM