I am struggling to use Injectable service insight another Injectable service in angular 5 Here is my crudService.ts :
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
}
);
}
}
Here is my authentication.service.ts where i am inserting 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);
}
}
and insight app.module.ts file I import only CrudService class :
import { CrudService } from "./common/services/crudService";
@NgModule({
imports: [
...
],
declarations: [
...
],
providers: [
...,
CrudService
],
bootstrap: [AppComponent]
})
my CrudService injectable export class insight app.module.ts file. Actually I tried different scenarious like import two classes : CrudService and AuthenticationService insight app.module.ts in the same time and other approaches... During compilation I don't have any errors but when my application is started i have error :
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!
Please if anybody have already solve the problem related to using one @Injectable() class insight another @Injectable() class and registration that logic insight app.module.ts for Angular 5, please send me the link or some scenario which is possible to follow to successfully compile, run and use @Injectable() classes. If i need precise something, please ask me and I will provide more details. Thanks
P.S. My AuthenticationService is injected insight 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 => {
});
}
}
AuthenticationService
? Because in app.module, you are only injecting theCrudService
so the error is telling you that there isNo provider for AuthenticationService!
– Alf MohAuthenticationService
. This normally means that you forgot to include it in theproviders
array of your module. – DeborahK