最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - NullInjectorError: No provider for ActivatedRoute - Stack Overflow

programmeradmin1浏览0评论

when I execute my angular application locally, this error occurred suddenly even without doing any code changes while the same code base can be run in a separate machine with the same configuration.

  • node version:v16.17.0

ERROR Error: Uncaught (in promise): NullInjectorError: R3InjectorError(AppModule)[ActivatedRoute -> ActivatedRoute -> ActivatedRoute]: NullInjectorError: No provider for ActivatedRoute!

when I execute my angular application locally, this error occurred suddenly even without doing any code changes while the same code base can be run in a separate machine with the same configuration.

  • node version:v16.17.0

ERROR Error: Uncaught (in promise): NullInjectorError: R3InjectorError(AppModule)[ActivatedRoute -> ActivatedRoute -> ActivatedRoute]: NullInjectorError: No provider for ActivatedRoute!

Share Improve this question asked Mar 20, 2023 at 7:42 Pubudu Mahesh MeththanandaPubudu Mahesh Meththananda 3976 silver badges17 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Try to add the following import in AppModule file

imports: [ 
    RouterModule.forRoot([]),
    ...
  ],

To import RouterModule you should do something like this:

import { RouterModule } from '@angular/router';

My friend removed provideRouter(routes), from appConfig when he added provideHttpClient()

Fixed this error by adding it back

// app.config.ts (Angular 18.x)
export const appConfig: ApplicationConfig = {
  providers: [
    provideHttpClient(),
    provideRouter(routes), // <-- added back
  ]
};
发布评论

评论列表(0)

  1. 暂无评论