【问题标题】:Need to add @Inject decorator manually in constructor需要在构造函数中手动添加@Inject 装饰器
【发布时间】:2018-05-22 13:07:51
【问题描述】:

我建立了一个新的 Angular 5 项目,似乎 @Injectable 装饰器不起作用。我必须为构造函数的每个参数使用 @Inject 装饰器手动注入。 我将 Angular 5.0.5 与 TS 2.4.2 一起使用。 这是我的 tsconfig.json:

{ "compileOnSave": false, "compilerOptions": { "outDir": "./dist/out-tsc", "sourceMap": true, "declaration": false, "moduleResolution": "node", "emitDecoratorMetadata": true, "experimentalDecorators": true, "target": "es5", "typeRoots": [ "node_modules/@types" ], "lib": [ "es2017", "dom" ] } }

我为此浪费了很多时间,我真的不明白会发生什么。 你知道这个问题吗?

【问题讨论】:

    标签: angular typescript


    【解决方案1】:

    我的猜测是您可能正在尝试注入接口:

    constructor(someDependency: SomeInterface) { /* ... */ }
    

    @inject 装饰器将是必需的。

    如果你想注入一个类:

    constructor(someDependency: SomeClass) { /* ... */ }
    

    @injectable 注释应该足够了。

    通过Is it possible to inject interface with angular2?了解更多信息

    如果这不是您的问题,那么您的项目可能有问题。也许您不止一次地导入了"reflect-metadata" 模块?

    【讨论】:

      【解决方案2】:

      我的错,我刚刚发现了问题...... 我在 polyfill.js 中评论了“core-js/es7/reflect”导入。

      谢谢你

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-09
        • 2017-05-04
        • 2012-11-17
        • 1970-01-01
        • 1970-01-01
        • 2019-05-16
        • 1970-01-01
        • 2019-08-04
        相关资源
        最近更新 更多