I am facing difficulties in utilizing an Injectable service within another Injectable service in Angular 5. Below is my crudService.ts file:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable()
export class CrudService
{
constructor(
private http: HttpClient
) { }
postItem(url: any, body: any, headers?: any, params?: any)
{
return this.http.post<any>(url,
body,
{
headers, params
}
);
}
}
Next is my authentication.service.ts where I am incorporating the crudService.ts file:
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams, HttpClientModule } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { CrudService } from "../common/services/crudservice";
@Injectable()
export class AuthenticationService {
constructor(
private http: HttpClient,
private crudService: CrudService
) { }
login(username: string, password: string) {
let url = "example";
let body = "example";
let headers = "example";
return this.crudService.postItem(url, body, headers);
}
}
In my app.module.ts file, only the CrudService class has been imported:
import { CrudService } from "./common/services/crudService";
@NgModule({
imports: [
...
],
declarations: [
...
],
providers: [
...,
CrudService
],
bootstrap: [AppComponent]
})
The CrudService @Injectable export class is included in the app.module.ts file. I have tested various scenarios like importing two classes (CrudService and AuthenticationService) into app.module.ts simultaneously among other approaches... Although no compilation errors occur, when launching my application, the following error arises:
ERROR Error: Uncaught (in promise): Error: StaticInjectorError(AppModule)[LoginComponent -> AuthenticationService]:
StaticInjectorError(Platform: core)[LoginComponent -> AuthenticationService]:
NullInjectorError: No provider for AuthenticationService!
Error: StaticInjectorError(AppModule)[LoginComponent -> AuthenticationService]:
StaticInjectorError(Platform: core)[LoginComponent -> AuthenticationService]:
NullInjectorError: No provider for AuthenticationService!
If anyone has successfully resolved issues related to using one @Injectable() class within another @Injectable() class and registering that logic in app.module.ts for Angular 5, kindly provide me with the necessary information or guidance to enable successful compilation, deployment, and use of @Injectable() classes. For further clarification or details, please feel free to ask. Thank you.
P.S. My AuthenticationService is injected into the LoginComponent class:
import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { AlertService, AuthenticationService } from '../_services/index';
import { CrudService } from "../common/services/crudservice";
@Component({
moduleId: module.id,
templateUrl: 'login.component.html'
})
export class LoginComponent implements OnInit {
model: any = {};
loading = false;
returnUrl: string;
constructor(
private route: ActivatedRoute,
private router: Router,
private authenticationService: AuthenticationService) { }
ngOnInit() {
}
login() {
this.loading = true;
this.authenticationService.login(this.model.username, this.model.password)
.subscribe(
resp => {
},
error => {
});
}
}