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 )
It looks like your @Injectable annotation is missing the providedIn
property.
Adding it should resolve the issue:
@Injectable({providedIn: 'root'})
export class LocationService {
...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.