Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
866 views
in Technique[技术] by (71.8m points)

Angular Resolver not working no provider for ()

I have this issue in a new project. I have created a route:

const routes: Routes = [
    {
        path: '',
        component: CategoriesComponent,
    },
    {
        path: ':categoryId',
        component: CategoryComponent,
        resolve: CategoryResolver,
    },
];

As you can see, it's trying to use a resolver, which looks like this:

import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, Resolve, RouterStateSnapshot } from '@angular/router';
import { Observable } from 'rxjs';

import { Category, CategoryService } from '@situlive/angular/data';

@Injectable({
    providedIn: 'root',
})
export class CategoryResolver implements Resolve<Category> {
    constructor(private categoryService: CategoryService) {}

    resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<Category> {
        return this.categoryService.get(route.paramMap.get('categoryId'));
    }
}

When I use this code, I get an error:

Uncaught (in promise): NullInjectorError: R3InjectorError(CategoriesModule)[() => [ -> () => [ -> () => [ -> () => [ -> () => []: NullInjectorError: No provider for () => [!

If I comment out the resolver like this:

const routes: Routes = [
    {
        path: '',
        component: CategoriesComponent,
    },
    {
        path: ':categoryId',
        component: CategoryComponent,
        //resolve: CategoryResolver,
        children: [
            {
                path: 'criteria',
                loadChildren: () => import('../criteria/criteria.module').then((m) => m.CriteriaModule),
            },
            {
                path: 'feeds',
                loadChildren: () => import('../feeds/feeds.module').then((m) => m.FeedsModule),
            },
            {
                path: 'fields',
                loadChildren: () => import('../fields/fields.module').then((m) => m.FieldsModule),
            },
        ],
    },
];

I no longer get the error, but obviously nothing is resolved. My code in my CategoryComponent just looks like this:

ngOnInit(): void {
    this.getCategory();
}

private getCategory(): void {
    this.category = this.route.snapshot.data.category;
}

Does anyone know why this might be happening?

question from:https://stackoverflow.com/questions/65937010/angular-resolver-not-working-no-provider-for

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You first need to fix your routes definition. That may be the problem. Try to change the:

    {
        path: ':categoryId',
        component: CategoryComponent,
        resolve: CategoryResolver,
    }

to :

    {
        path: ':categoryId',
        component: CategoryComponent,
        resolve: {
          category: CategoryResolver
        },
    }

Because the resolve needs to be an object and not the direct reference to the resolver as you can se here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...