@Inject does not add metadata when in AoT
See original GitHub issueI have some code below, the problem is: it working in JIT mode, and when I run ng serve -aot, the generated code for class Computer dose not have metadata for DI work. So an error occur like this:
Cannot resolve all parameters for 'Computer'(?). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'Computer' is decorated with Injectable.
Code example
import { ReflectiveInjector, Inject, Injectable } from '@angular/core';
class CPU {
constructor() {}
start() {
console.log('CPU speed 3.2GHz');
}
}
class Computer {
constructor(@Inject(CPU) public cpu: CPU) {}
}
function context() {
const injector = ReflectiveInjector
.resolveAndCreate([Computer, CPU]);
const computer = injector.get(Computer);
computer.cpu.start();
}
context();
I’m submitting a … (check one with “x”)
[x] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
Current behavior It cannot resolve dependency when in AoT, but it work in JIT mode.
Expected behavior working in both.
Minimal reproduction of the problem with instructions
What is the motivation / use case for changing the behavior?
Please tell us about your environment:
-
Angular version: @angular/cli: 1.0.0 @angular/common: 4.0.2 @angular/compiler: 4.0.2 @angular/core: 4.0.2 @angular/forms: 4.0.2 @angular/http: 4.0.2 @angular/platform-browser: 4.0.2 @angular/platform-browser-dynamic: 4.0.2 @angular/router: 4.0.2 @angular/cli: 1.0.0 @angular/compiler-cli: 4.0.2
-
Browser: [all | Chrome XX | Firefox XX | IE XX | Safari XX | Mobile Chrome XX | Android X.X Web Browser | iOS XX Safari | iOS XX UIWebView | iOS XX WKWebView ]
-
Language: [all | TypeScript X.X | ES6/7 | ES5]
-
Node (for AoT issues):
node --version= v6.10.0
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (4 by maintainers)

Top Related StackOverflow Question
@tieppt That’s not allowed any more, see https://github.com/angular/angular/issues/13820 , if it works in JIT, then it’s a JIT bug.
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.