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"

5
  • Please post your UserService; is it decorated with @Injectable()? (Unless it's exactly what's in the example Plunker of course...) Commented Dec 14, 2016 at 15:24
  • Also, your import path might be wrong: you use /Common in one and /common right below... Commented Dec 14, 2016 at 15:25
  • 1
    @msanford post updated, User service added. paths are fine,as imported classes are appearing in visual studio intellisense. Commented Dec 14, 2016 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. Commented Dec 16, 2016 at 9: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. Commented Dec 16, 2016 at 12:55

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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!
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.

2 Comments

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.
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.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.