Angular 8 Resolver not functioning properly: Resolver provider not found

My router, which utilizes a resolver, is currently experiencing issues.

The error message reads:

Uncaught (in promise): NullInjectorError: StaticInjectorError(AppModule)[BlogResolver]: StaticInjectorError(Platform: core)[BlogResolver]: NullInjectorError: No provider for BlogResolver!

Here is the code for the resolver's function:

resolve(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<any> {
  const id = route.params.id;
  return this.service.findById(
}

This is how the router is defined:

{
path: 'blog/:id',
component: BlogpostComponent,
resolve: {
  blogpost: BlogResolver,
},

I have never come across this error before and haven't been able to find a similar solution. Any assistance would be greatly appreciated.

Edit:

app.module

@NgModule({
  declarations: [
    AppComponent,
    FooterComponent,
    NavbarComponent,
    HomeComponent,
    NotFoundComponent,
  ],
  imports: [
    BrowserModule,
    HttpClientModule,
    FormsModule,
    ReactiveFormsModule,
    NgbModule,
    AppRoutingModule,
    MinhaEmpresaModule,
  ],
  providers: [],
  bootstrap: [AppComponent],
})
export class AppModule {}

child module:

@NgModule({
  declarations: [
    AEmpresaComponent,
    ComoChegarComponent,
    TrabalheConoscoComponent,
    BlogComponent,
    BlogpostComponent,
  ],
  imports: [
    CommonModule,
    ReactiveFormsModule,
    AEmpresaRoutingModule,
    NgbPaginationModule,
  ],
})

Answer №1

It appears that you have forgotten to include the BlogResolver in your code. You can add it in either the

AppModule:

@NgModule({
  imports:      [ BrowserModule, FormsModule, AppRoutingModule ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ],
  providers: [ BlogResolver ]  // **** DON'T FORGET TO ADD THIS LINE ****
})
export class AppModule { }

or within the BlogResolver itself:

@Injectable({
  providedIn: "root"  // **** DON'T FORGET TO ADD THIS LINE ****
})
export class BlogResolver implements Resolve<Somedata> {
  data: Somedata[] = [{ id: 1, name: "ABC" }];

If you would like to see a similar error message and resolution, you can check out this Stackblitz example: https://stackblitz.com/edit/angular-m8azja

Answer №2

If you're looking to set up the resolver, there are a couple of methods you can use.

  1. One way is to include the Injectable({providedIn: 'root'}) decorator when declaring the resolver.
@Injectable({
  providedIn: 'root',
})
export class BlogResolver implements Resolve<Blog> {
  constructor(private bs: BlogService, private router: Router) {}

  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<Blog> {
  ...
  }
}

  1. Alternatively, you can add the resolver to the providers array in your routing module.
const routes: Routes = [{
  path: 'blog/:id',
  component: BlogpostComponent,
  resolve: {
    blogpost: BlogResolver,
  }
}];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  providers: [
    BlogResolver // <-- Don't forget to add the resolver here.
  ],
  exports: [RouterModule]
})
export class BlogRoutingModule {}

Remember to also include the BlogRoutingModule in either the feature or main app module.

Answer №3

During my search for a solution to a familiar error, I discovered that although I had provided the correct resolver, the issue was with how I defined it within my route:

{
  // ...
  resolve: BlogResolver
}

Instead of specifying it correctly as:

{
  // ...
  resolve: {
    blogpost: BlogResolver
  }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The `HubConnection` class constructor is restricted to private access and can only be accessed within its class declaration

Currently, I am in the process of integrating an Angular and SignalR project. The guidance I am following comes from a resource on Medium. I have successfully installed all essential packages and included the code snippet below in my app.component.ts file ...

Steps for confirming a property setting on an interface

I am working with the following interface export interface Command { id: CommandId; disabled: boolean; } My goal is to verify that the 'disabled' property has been changed. Here are my attempts: 1) Creating an object and checking if t ...

The use of props within components is broken in the interface of Nuxt and Vuejs

I am having trouble accessing an object's interface within a component using props. Is there anyone who can provide guidance on how to resolve this issue? PortariaInterface define interface PortariaInterface { entryDate: string nfe?: { numbe ...

Using CKEditor5 to Capture and Edit Key Presses

I'm currently working on capturing input from a CKEditor5 within an Angular application using TypeScript. While I am able to successfully display the CKEditor and confirm its presence through logging, I am facing difficulties in capturing the actual i ...

navigating through different pages on a react-native application

While building my app in react-native, I encountered some issues with setting up the navigation. I referred to the react-native documentation for guidance, specifically this navigation guide. The guide mentioned passing an object named "navigation" to your ...

How can an array be generated functionally using properties from an array of objects?

Here's the current implementation that is functioning as expected: let newList: any[] = []; for (let stuff of this.Stuff) { newList = newList.concat(stuff.food); } The "Stuff" array consists of objects where each ...

Angular service is continuously throwing the error message "NullInjectorError: No provider for anotherService"

I recently built a basic Angular service and encountered an issue. @Injectable() export class someHandler { constructor( public anotherService: anotherService, ) {} ... The problem arises when I try to use this service in a component, as ...

Removing HTML Tags in Ionic - A How-To Guide

After utilizing Ionic 3 to retrieve data from a WordPress API, I am encountering an issue with displaying the content on the app UI. The problem arises from the presence of HTML tags within the content being printed along with the text. While seeking solut ...

What is the primary function of the platform-server module within Angular 2?

While exploring the AOT (ahead of time compilation) documentation at https://angular.io/docs/ts/latest/cookbook/aot-compiler.html#!#compile, I noticed a dependency on platform-server. What exactly is the function of this platform, when it seems like only ...

What is the best way to activate the dirty flag of an Angular form using JavaScript?

I need to update the dirty flag of a form using JavaScript by accessing its DOM element. In order to do this, I am using @ViewChild to retrieve the element and then retrieving it in the ngOnInit lifecycle method (there are different ways to achieve this s ...

Tips for integrating Less and Bootstrap in an Angular 6 project

After making changes to angular.json to use less as the styleext, I tested it by creating a component. However, now I want to incorporate Bootstrap classes into my component's css/less. I specifically want all buttons in my component to have the .mx-1 ...

NestJS is having trouble importing generated types from the Prisma client

When working with Prisma in conjunction with NestJs, I encountered an issue after defining my model and generating it using npx prisma generate. Upon importing the generated type, I can easily infer its structure: import { FulfilmentReport, FulfilmentRepor ...

Frozen objects in Typescript 2 behave in a variety of ways depending on their shape

Let's say I'm working with an object whose inner structure is unknown to me because I didn't create it. For instance, I have a reference to an object called attributes that contains HTML attributes. I then made a shallow copy of it and froze ...

Encountering a problem with ng-select in Angular 5: The following error occurred - TypeError: Object(...) is not a function at NgDropdownPanelComponent.ngOnInit within

I am currently using ng-select 2.0.0 in my Angular 5 project, following the guidelines provided in this documentation. Although I am able to retrieve results from a REST API in Spring Boot, I have encountered an issue when clicking on ng-select: ERROR Typ ...

Is transitioning from AngularJS to Angular truly imperative?

We currently have a project built with AngularJS, but its support is ending and there are potential security vulnerabilities (CVEs) present. Do we need to transition to a different framework? Our project primarily involves front end development with data b ...

Angular2: How to deactivate a specific element in a form created with formbuilder

After initializing my formbuilder, I need to disable a specific element because I need to perform some validation once the view is loaded. Here is how I declared my formBuilder. ionViewDidLoad() { this.purchaseDataForm = this.formBuilder.group({ kms ...

Having trouble with Nestjs Global Validation Pipe when parsing Boolean query parameters?

Within this controller, I am attempting to pass a boolean parameter in the GET call. @Controller('tests') export class TestController { constructor(private readonly testService: TestService) {} @Get() async getTests(@Query() params: ...

Input for uncomplicated changing identifier

I am looking to create types for dynamic keys that will be of type number. I have two similar types defined as follows: type UseCalculatePayments = () => { totalPayments: number; aggregate: number; condition: boolean; }; type UseCalculateCommissio ...

Attempting to send numerous identifiers in an API request

I encountered a problem while working on a function in Angular that involves pulling data from an API. My goal is to enhance a current segment to accommodate multiple IDs, but I face difficulties when attempting to retrieve more than one ID for the API que ...

Improving efficiency with batch processing in angular-apollo-client

How can I enable batching in the Angular Apollo client? I'm having trouble passing the shouldbatch=true parameter to the constructor of the Angular Client. ...