question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@Inject does not add metadata when in AoT

See original GitHub issue

I 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:

  • Language: [all | TypeScript X.X | ES6/7 | ES5]

  • Node (for AoT issues): node --version = v6.10.0

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
trotylcommented, Apr 21, 2017

@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.

0reactions
angular-automatic-lock-bot[bot]commented, Sep 11, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

AOT metadata errors - Angular
The following are metadata errors you may encounter, with explanations and ... Adding export doesn't work when the compiler needs the actual value...
Read more >
Getting error while trying to migrate to Angular 9 - Stack Overflow
The following project failed: libs/core/tsconfig.lib.json Error: ~/core-routing.module.ts:138:1: Error encountered in metadata generated for ...
Read more >
Core Technologies - Spring
AOT processing can be used to optimize your application ahead-of-time. ... XML-based metadata is not the only allowed form of configuration metadata.
Read more >
The Ahead-of-Time (AOT) compiler - Angular
Do not refer to ambient types in metadata expressions. If you must inject an instance of an ambient type, you can finesse the...
Read more >
Collect Metadata with the Tracing Agent - Oracle Help Center
However, the analysis cannot always completely predict all usages of the Java Native Interface (JNI), Java Reflection, Dynamic Proxy objects, or class path ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found