Angular Error : Staticinjectorerror (platform: Core)[e -> T]:
Solution 1:
You are trying to use a service that is not listed in providers of your AppModule or inside you component.ts. Add the service to a providers list to make it work.
In app.modules if you want that service to be global (related to app context).
@NgModule({
declarations: [...],
imports: [...],
bootstrap: [...],
entryComponents: [...],
providers: [
MyService
]
})
Or in your component.ts if you want that service to be contextual to desired component.
@Component({
selector: '...',
templateUrl: '...',
providers: [MyService]
})
Do not add it in both files. Also don't forget to import that service when you inject it either in app.modules or component.
import { MyService } from'../services/myservice';
This question also may help you: Error: No provider for t
Solution 2:
This happens because you did not add the services used to build the module
example:
1. component
@Component({
selector: 'app-sample',
templateUrl: './sample.component.html',
styleUrls: ['./sample.component.scss']
})
exportclassSampleComponentimplementsOnInit {
constructor(private localServiceName: YourService) {
}
ngOnInit() {
}
}
2. module
@NgModule({
declarations: [SampleComponent]
})
change to
@NgModule({
declarations: [SampleComponent]
providers: [ YourService ]
})
Solution 3:
For anyone who could need this:
The reasons stated above are one case scenario where StaticInjectorError could be thrown. However, it's not the only situation. In other cases, it could happen because you are trying to inject a dependency that is undefined. That is: a) "Dumb case":You haven't imported the service, but you injected anyway. Mosts IDE's will catch that for you b) Complicated case: say you have custom libraries that depends ones from another and you don't set the right peer dependencies in each of them. This will cause inconsistency between modules (different version of the same library in your app). On top of that, if you use Ahead-of-time compilation, it might happen that the service you try to import is not reached from the proper module (library version). This will cause that the service = undefined, hence, it would be like case a)
Talking from my own experience!
Post a Comment for "Angular Error : Staticinjectorerror (platform: Core)[e -> T]:"