0

I have an angular app (v10.1) that is using routing.

In short, I am using modules to take advantage of lazy loading the basic dir structure looks like this

ROOT
    /modules
        /myMod
            myMod.component.ts

the myMod component needs to make use of some services, and so I am trying to include the service (location.service) and declare it in the constructor.

I get an error

core.js:4352 ERROR Error: Uncaught (in promise): 
    NullInjectorError: R3InjectorError(myModModule)[LocationService -> LocationService -> LocationService -> LocationService -> LocationService]: 
    NullInjectorError: No provider for LocationService!

I have tried to add this service to a provider array in @NgModule BUT I have 3 modules to consider ROOT,Resource,myMod

I have tried adding the service to each of these respectively, to no avail - still get the error

Here is a little bit more detailed diagram of the app Please dont tell me I need to add the service to each of the mods, and if Im missusing modules can someone please explain. Thx

ROOT
-app.module
-app-routing.module
-app.component.html (has link routerLink="/resources/myMod" )

-- modules/resources
----resources-routing.module.ts
        const routes: Routes = [{
                path: 'myMod', loadChildren: () => import('../myMod/myMod.module').then((m) => m.myModModule),
            }]

-- modules/myMod
----myMod.module.ts
        import { myModRoutingModule } from './myMod-routing.module';
        import { myModComponent } from './myMod.component';
        @NgModule({
            declarations: [myModComponent],
            imports: [CommonModule, myModRoutingModule]
        })
        export class myModModule {}

----myMod.component.ts
        import { LocationService } from '../../common/services/location.service';
        @Component({
            selector: 'app-myMod',
            templateUrl: './myMod.component.html'
        })
        export class myModComponent implements OnInit {
            constructor( private locationService: LocationService )

https://stackblitz.com/edit/angular-injection-error1

enter image description here

2
  • Please include the service class as well, and what version of Angular you're building in. The most likely culprit is that your @Injectable annotation is either missing or is missing the providedIn property. Commented Feb 17, 2021 at 1:13
  • @El-Mo - v10.1 - service above Commented Feb 17, 2021 at 1:23

1 Answer 1

1

It looks like your @Injectable annotation is missing the providedIn property.

Adding it should resolve the issue:

@Injectable({providedIn: 'root'})
export class LocationService {
  ...
}
Sign up to request clarification or add additional context in comments.

9 Comments

El-mo - that worked - until I added import { HttpClient, HttpHeaders, HttpErrorResponse } from '@angular/common/http'; now that doesnt even work
Is there an error on the console associated with the imports?
Uncaught (in promise): NullInjectorError: R3InjectorError(CollegesModule)[CollegeService -> CollegeService -> CollegeService -> HttpClient -> HttpClient -> HttpClient]: NullInjectorError: No provider for HttpClient! - this is a college service, loaded right after the location service, that uses httpClient to call the API.
Please update your post with a stackblitz that reproduces the issue.
well, I cant even get Router working - stackblitz.com/edit/angular-qzdrng
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.