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

前端 未结 2 1342
忘掉有多难
忘掉有多难 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:36

    You code is fine!

    Here is your updated plunker: https://plnkr.co/edit/6SR8Ibljuy0ElEBU87ox?p=preview

    Did some changes to your system.js.config!

       // ADDED THESE TWO OPTIONS BELOW
    
       //use typescript for compilation
       transpiler: 'typescript',
       //typescript compiler options
       typescriptOptions: {
          emitDecoratorMetadata: true
       },
    

    and this..

    app: {
       main: './main.ts',     // CHANGES HERE
       defaultExtension: 'ts' // CHANGES HERE
    },
    
    0 讨论(0)
  • 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
    0 讨论(0)
提交回复
热议问题