angularjs 2.0: Can't inject anything through component constructor()

前端 未结 2 1349
忘掉有多难
忘掉有多难 2020-12-12 05:50

I am creating a sample application in angularjs 2.0. While developement I came across with a serious problem - I can\'t inject anything to the component through the construc

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-12 06:49

    I see that your tsconfig.json isn't correct.

    { 
      "compilerOptions": { 
        "target": "es5", 
        "module": "commonjs", 
        "moduleResolution": "node", 
        "sourceMap": false, 
        "emitDecoratorMetadata": true, <== it should be true 
        "experimentalDecorators": true, 
        "removeComments": false, 
        "noImplicitAny": false, 
        "outDir": "dist" 
      } 
    } 
    

    This is the magic sauce for DI. emitDecoratorMetadata: true. This option will preserve type information in your object's metadata.

    See also

    • How does the TypeScript Angular DI magic work?
    • Injecting services in services in Angular 2

提交回复
热议问题