Currently in the process of constructing an application using Angular 6, while still in the setup phase. Encountering difficulties with dependency injection within my application.
The app is unable to resolve any constructor parameters resulting in
Uncaught Error: Can't resolve all parameters for AppComponent: (?).
. Even a custom service triggers the same error.
Versions (omitting dependencies that have no impact on this issue)
"dependencies": {
"@angular/common": "6.0.5",
"@angular/compiler": "6.0.5",
"@angular/core": "6.0.5",
"@angular/forms": "6.0.5",
"@angular/http": "6.0.5",
"@angular/platform-browser": "6.0.5",
"@angular/platform-browser-dynamic": "6.0.5",
"@angular/router": "6.0.5",
"core-js": "2.5.7",
"reflect-metadata": "0.1.12",
"rxjs": "6.2.1",
"zone.js": "0.8.26"
},
"devDependencies": {
"@angular/compiler-cli": "6.0.5",
"@ngtools/webpack": "6.0.8",
"angular2-template-loader": "0.6.2",
"awesome-typescript-loader": "5.1.0",
"typescript": "2.7.2",
"webpack": "4.12.0",
"webpack-cli": "3.0.8",
"webpack-dev-server": "3.1.4",
}
app.module.ts
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {
}
TestService.ts
import {Injectable} from "@angular/core";
@Injectable({
providedIn: 'root'
})
export class TestService {
constructor() {
console.warn("It works!");
}
public sayHello(): string {
return "hello world!";
}
}
App.component.ts
import {Component} from '@angular/core';
import {TestService} from "./TestService";
@Component({
selector: 'sh-home',
styleUrls: ['./home.scss'],
templateUrl: './home.html'
})
export class HomeComponent {
constructor(testService: TestService) {
testService.sayHello();
}
}
Error occurs when injecting the TestService
.
Main.ts
import {enableProdMode} from '@angular/core';
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import {AppModule} from './app/app.module';
import './assets/scss/styles.global.scss'; // Import the global scss files
// Polyfills
import './Polyfills';
if (process.env.NODE_ENV === 'production') {
enableProdMode();
}
platformBrowserDynamic().bootstrapModule(AppModule);
Polyfills.ts
/** IE9, IE10 and IE11 requires all of the following polyfills. **/
import 'core-js/es6/symbol';
import 'core-js/es6/object';
import 'core-js/es6/function';
import 'core-js/es6/parse-int';
import 'core-js/es6/parse-float';
import 'core-js/es6/number';
import 'core-js/es6/math';
import 'core-js/es6/string';
import 'core-js/es6/date';
import 'core-js/es6/array';
import 'core-js/es6/regexp';
import 'core-js/es6/map';
import 'core-js/es6/weak-map';
import 'core-js/es6/set';
/** IE10 and IE11 requires the following for NgClass support on SVG elements */
import 'classlist.js'; // Run `npm install --save classlist.js`.
/** IE10 and IE11 requires the following for the Reflect API. */
import 'core-js/es6/reflect';
/** Evergreen browsers require these. **/
// Used for reflect-metadata in JIT. If you use AOT (and only Angular decorators), you can remove.
import 'core-js/es7/reflect';
/***************************************************************************************************
* Zone JS is required by default for Angular itself.
*/
import 'zone.js/dist/zone'; // Included with Angular CLI.
Not utilizing the CLI but a custom starter project. All necessary polyfills included similar to Angular-CLI implementation, ruling out any missing components.
Analyze and advise on potential missteps taken?
Update
Trimmed down the test scenario and evidently, the translation module is not at fault. Simple service creation still fails to utilize dependency injection. Adding the service to the providers
list proves futile as well, despite utilizing Angular 6 'provideIn: root
'.