繁体   English   中英

Angular 5:无法进行依赖注入

[英]Angular 5: cannot get dependency injection working

我有一个角度5(“ @ angular / core”:“ 5.1.2”)应用,但我不断收到此错误

未捕获的错误:无法解析FooComponent的所有参数:(?)。

服务:

@Injectable()
export class FooService { }

零件:

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

模块:

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

我的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"
    ]
}

知道我缺少什么吗? 请注意,我使用的是es6 lib, @Injectable没有任何参数。 我似乎找不到很多好的例子。

通常我们如何在角度上调试DI?

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

错过了@Inject

暂无
暂无

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

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