The same code worked for me in angular 8 but now it's giving me this error. "The class 'BaseService' cannot be created via dependency injection, as it does not have an Angular decorator. This will result in an error at runtime.
Either add the @Injectable() decorator to 'BaseService', or configure a different provider (such as a provider with 'useFactory')."
I'm only trying to achieve simple inheritence here.
1) BaseService.ts (parent class)
import { environment } from 'src/environments/environment';
import { HttpHeaders } from '@angular/common/http';
export class BaseService {
url
constructor(postfixUrl) {
this.url = environment.backendUrl + postfixUrl
}
setUpHeaders() {
return {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
}
}
}
2) AuthService.ts (child class)
import { Injectable } from "@angular/core";
import {HttpClient} from '@angular/common/http'
import { BaseService } from './base.service';
@Injectable({
providedIn: 'root'
})
export class AuthService extends BaseService {
url
constructor(private http: HttpClient) {
super('auth')
}
register(user) {
return this.http.post(this.url, user, this.setUpHeaders())
}
}
3) auth.module.ts
@NgModule({
declarations: [
AuthComponent,
LoginComponent,
RegisterComponent
],
imports: [
CommonModule,
AuthRoutingModule,
HttpClientModule,
FormsModule,
ReactiveFormsModule
],
providers: [AuthService, BaseService]
})
export class AuthModule { }
the error
The @Injectable decorator should be added to any service that uses dependency injection (DI). The @Injectable decorator is not compulsory to add if you don't use the 'providedIn' option.
Injectable() decorator is used to inject other services or objects into your service. If your service do not have any dependencies then you don't need to add Injectable() decorator.
The @Injectable() decorator defines a class as a service in Angular and allows Angular to inject it into a component as a dependency. Likewise, the @Injectable() decorator indicates that a component, class, pipe, or NgModule has a dependency on a service. The injector is the main mechanism.
The Inject decorator is a constructor parameter used to specify a custom provider of a dependency.
If you want to use a service for a single module then you do not need to use @Injectable({ providedIn: 'root' })
.
Two steps:
@Injectable()
add in your service.providers: [ BaseService ]
add in your module inner NgModule
.You have added BaseService
to the module providers. BaseService
accepts the parameter postfixUrl
in its constructor.
You should remove at least the BaseService
from the providers, since Angular won't know how to resolve the parameters.
Adding a service to the module providers means that an instance of that service will be shared within that module. Adding @Injectable({ providedIn: 'root' })
means it will be shared within the whole app.
You 2 competing methods of registering services for dependency injection, and registering BaseService
is invalid.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With