Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 no provider for service error

Tags:

From Angular2 access global service without including it in every constructor I have grabbed the code and slightly modified it into:

@Injectable()
export class ApiService {
  constructor(public http: Http) {}

  get(url: string) {
    return http.get(url);
  }

}

@Injectable()
export abstract class BaseService {
  constructor(protected serv: ApiService) {}  
}

@Injectable()
export class MediaService extends BaseService {

  // Why do we need this?
  constructor(s: ApiService) {
    super(s)
  }

  makeCall() {
    this.serv.get("http://www.example.com/get_data")
  }

}

However, when I try to run this code I get an error in the console:

NoProviderError {message: "No provider for ApiService! (App -> MediaService -> ApiService)", stack: "Error: DI Exception↵

I have created a Plunkr with the code at https://plnkr.co/edit/We2k9PDsYMVQWguMhhGl

Does anyone know what causes this error?

like image 498
Bas van Dijk Avatar asked Mar 17 '16 14:03

Bas van Dijk


People also ask

How to fix nullinjectorerror no provider for httpclient in angular?

To fix NullInjectorError: No provider for HttpClient! error in Angular follow the below steps. Open app.module.ts file of Angular Application. Import HttpClientModule from @angular/common/http. Add HttpClientModule to the @NgModule imports array.

Is there a way to add providers in Angular 2?

@nathanhleung it seems like providers made it's way into the release also. In Angular 2 there are three places you can "provide" services: "The bootstrap provider option is intended for configuring and overriding Angular's own preregistered services, such as its routing support."

What is the use of providedin in angular?

By default, this decorator has a providedIn property, which creates a provider for the service. In this case, providedIn: 'root' specifies that Angular should provide the service in the root injector. Show activity on this post.

What is @injectable service in angular?

The service itself is a class that the CLI generated and that's decorated with @Injectable (). By default, this decorator has a providedIn property, which creates a provider for the service. In this case, providedIn: 'root' specifies that Angular should provide the service in the root injector.


1 Answers

You need to also define ApiService in the providers attribute.

@Component({
  selector: 'my-app',
  providers: [MediaService, ApiService], // <-----
  template: `
    <div>
      <h2>Hello {{name}}</h2>
    </div>
  `,
  directives: []
})
export class App {
  constructor(mediaService: MediaService) {
    this.name = 'Angular2'

    console.log('start');

    mediaService.makeCall();
  }
}

It's because injectors are relied on components. If you want to have more details about how to inject a service into a service, you could have a look at this question:

  • What's the best way to inject one service into another in angular 2 (Beta)?
like image 101
Thierry Templier Avatar answered Oct 09 '22 08:10

Thierry Templier