I am trying to build an application with Angular 6 and I am still setting everything up. But it seems there is something wrong with the dependency injection in my app.
I also experienced the same issue with Angular 8.2.13 + Typescript. Finally the trick was to use a @Inject('RefOnlyUsedForTesting')
, even if the type is string
.
export abstract class MyBaseClass {
....
constructor(@Inject(ElementRef) elementRef: ElementRef,
@Optional() @Inject('RefOnlyUsedForTesting') dep1: MyObject,
@Optional() @Inject('RefOnlyUsedForTesting') dep2: string,
@Optional() @Inject('RefOnlyUsedForTesting') dep3: string) {
super();
...
}
}
I received the same error on Angular 8 app
Can't resolve all parameters for AppComponent: (?,?,?)
after changing 1 parameter type in the constructor().
I solved the problem by stop and restarting the app.
@Inject did the trick for me
import {Component, Inject} from '@angular/core';
import {TestService} from "./TestService";
@Component({
selector: 'sh-home',
styleUrls: ['./home.scss'],
templateUrl: './home.html'
})
export class HomeComponent {
constructor(@Inject(TestService) testService: TestService) {
testService.sayHello();
}
}
Make sure you have following import in polyfills:
import 'core-js/es7/reflect';
If you are using webpack and babel to build Angular, it is likely you are missing this babel plugin: babel-plugin-transform-typescript-metadata
I also got this issue but setting emitDecoratorMetadata": true in tsconfig solve the issue. Remember to restart the server.
tsconfig.spec.json
"compilerOptions": { "emitDecoratorMetadata": true, "outDir": "./out-tsc/spec",