5

I have an app module and single component application (made to demonstrate my problem), and getting following error:

 Error in ./AppComponent class AppComponent_Host - inline template:0:0 caused by: No provider for UserService! ; Zone: <root> ; Task: Promise.then ; Value:

code for AppModule:

import { NgModule }       from '@angular/core';
import { BrowserModule }  from '@angular/platform-browser';
import { UserService } from './components/common/userservice';

@NgModule({
    imports: [
    BrowserModule,
],
declarations: [
    AppComponent
],
providers: [UserService],
bootstrap: [AppComponent],
entryComponents: []

})
export class AppModule {
}

Code for my AppComponent:

import { Component} from '@angular/core';
import { UserService} from './userservice';

@Component({
  selector: 'App',
  template: `<h3>App component</h3>                                                                                      
            user name: {{userName}}
            `,
  providers: []
  })

export class AppComponent  {

  userName: string;
  constructor(userService: UserService) {
      this.userName = userService.userName;   
  }    
}

My UserService Code:

import { Injectable, EventEmitter } from '@angular/core';
@Injectable()
export class UserService {
    obs$: EventEmitter<any> = new EventEmitter<any>()
    userName = 'Sherlock Holmes';
}

Now if i add UserService as provider to AppComponent, it will solve the issue. but i dont want to, because i want only one instance of my service in whole application. Even in subModules(feature modules).

according to my understanding, if i add service as provider on module level, then i can just inject it to any component under module.

here is am example i was watching.

Plunker

am using angular2 version: "2.0.0"

Fazal Wahid
  • 175
  • 2
  • 15
  • Please post your `UserService`; is it decorated with `@Injectable()`? (Unless it's exactly what's in the example Plunker of course...) – msanford Dec 14 '16 at 15:24
  • Also, your import path might be wrong: you use `/Common` in one and `/common` right below... – msanford Dec 14 '16 at 15:25
  • 1
    @msanford post updated, User service added. paths are fine,as imported classes are appearing in visual studio intellisense. – Fazal Wahid Dec 14 '16 at 16:07
  • 1
    @msanford your were right actually. path is case sensitive. Visaul studio accepts it but it wont work in application. you can post answer so that i can mark it as answer. – Fazal Wahid Dec 16 '16 at 09:35
  • Done! Also, that's annoying. The whole purpose of using IDEs is to find stuff like this for you. I've had good success with Webstorm, if that's an option. – msanford Dec 16 '16 at 12:55

3 Answers3

6

The import path is wrong: you use /Common in one and /common right below.

Visual Studio and WebStorm will not show IntelliSense errors for case-sensitivity of paths.

Furthermore, if using Angular 5's AoT template compilation, you can get a "This component is not part of a module" error, even though it is, because the import path is incorrect. Without AoT this will work, so you'll get a surprise when converting to AoT.

msanford
  • 10,127
  • 8
  • 56
  • 83
  • 1
    So annoying. Took me hours looking at barrel and provider setup and it turned out to be a case sensitive issue with a service in a barrel. Thanks! – GraemeMiller Apr 11 '17 at 22:37
0

Remove your service: UserService from app.module.ts, then add in component:

@Component({
  selector: 'App',
  template: `<h3>App component</h3>                                                                                      
        user name: {{userName}}
        `,
  providers: [UserService]
})

Hope this will help you.

Avnesh Shakya
  • 3,502
  • 2
  • 19
  • 28
  • 1
    Yes, for that you have to inject your **service** `providers: [UserService]` in `app.component.ts` (root component) which will be available in whole your application. – Avnesh Shakya Dec 14 '16 at 15:31
  • 1
    as i mentioned in post. I dont want to add it to component providers. If i added it in module, it should be injected in component automatically. – Fazal Wahid Dec 14 '16 at 16:10
0

An additional reason for getting this error - I duplicated a service to a different directory and was updating each file individually. Even though the first file still existed I got this error until I updated app.module.ts.

Robin Schaaf
  • 145
  • 1
  • 10