Angular 9 : Error NG2003: No suitable injection token for parameter 'url' of class 'DataService'. Found string Angular 9 : Error NG2003: No suitable injection token for parameter 'url' of class 'DataService'. Found string angular angular

Angular 9 : Error NG2003: No suitable injection token for parameter 'url' of class 'DataService'. Found string


In DataService.ts :

Update the Constructor .

//import { Inject } from '@angular/core';constructor(@Inject(String) private url: string, private http: Http)

UPDATED (Explanation):

According to https://angular-2-training-book.rangle.io/di/angular2/inject_and_injectable ;

@Inject() is a manual mechanism for letting Angular know that a parameter must be injected.

@Inject decorator is only needed for injecting primitives.

The primitive types are number, string, boolean, bigint, symbol, null, undefined .

The other (alternative) way can be used is:

//import { Inject } from '@angular/core';@Inject('url') private url: string;


As I encounter with same error myself, with same notation I believe you've watched 'Mosh' angular course which was belong to version 4.

There were several changes since then, one of them is replacing Http with HttpClient, another one was huge changes in rxjs library, needless to say same happened for services .Now instead of putting your services in providers you use @Injectable method which I think is more cleaner and more related to the service itself.

However if you had close attention to implementation you'll notice that DataService is just a base class and it would never be use as a service itself.It's like a manifest.So, it does not need to be injected or provided in app module. If you can recall 'Mosh' never did provide it in App module.The only service that was provided was PostService. That's why you should remove @Injectable decoration from DataService.