Angular 解析器无法正常工作 ()
Angular Resolver not working no provider for ()
我在一个新项目中遇到了这个问题。
我创建了一条路线:
const routes: Routes = [
{
path: '',
component: CategoriesComponent,
},
{
path: ':categoryId',
component: CategoryComponent,
resolve: CategoryResolver,
},
];
如您所见,它正在尝试使用如下所示的解析器:
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'));
}
}
当我使用这段代码时,出现错误:
Uncaught (in promise): NullInjectorError: R3InjectorError(CategoriesModule)[() => [ -> () => [ -> () => [ -> () => [ -> () => []:
NullInjectorError: No provider for () => [!
如果我这样注释掉解析器:
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),
},
],
},
];
我不再收到错误,但显然什么都没有解决。
我的 CategoryComponent 中的代码如下所示:
ngOnInit(): void {
this.getCategory();
}
private getCategory(): void {
this.category = this.route.snapshot.data.category;
}
有谁知道为什么会这样?
您首先需要修正您的路线定义。这可能是问题所在。
尝试更改:
{
path: ':categoryId',
component: CategoryComponent,
resolve: CategoryResolver,
}
至:
{
path: ':categoryId',
component: CategoryComponent,
resolve: {
category: CategoryResolver
},
}
因为 resolve 需要是一个对象,而不是像您可以看到的那样直接引用 resolver here
我在一个新项目中遇到了这个问题。 我创建了一条路线:
const routes: Routes = [
{
path: '',
component: CategoriesComponent,
},
{
path: ':categoryId',
component: CategoryComponent,
resolve: CategoryResolver,
},
];
如您所见,它正在尝试使用如下所示的解析器:
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'));
}
}
当我使用这段代码时,出现错误:
Uncaught (in promise): NullInjectorError: R3InjectorError(CategoriesModule)[() => [ -> () => [ -> () => [ -> () => [ -> () => []: NullInjectorError: No provider for () => [!
如果我这样注释掉解析器:
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),
},
],
},
];
我不再收到错误,但显然什么都没有解决。 我的 CategoryComponent 中的代码如下所示:
ngOnInit(): void {
this.getCategory();
}
private getCategory(): void {
this.category = this.route.snapshot.data.category;
}
有谁知道为什么会这样?
您首先需要修正您的路线定义。这可能是问题所在。 尝试更改:
{
path: ':categoryId',
component: CategoryComponent,
resolve: CategoryResolver,
}
至:
{
path: ':categoryId',
component: CategoryComponent,
resolve: {
category: CategoryResolver
},
}
因为 resolve 需要是一个对象,而不是像您可以看到的那样直接引用 resolver here