gpt4 book ai didi

javascript - 创建本地路由和解析器时的困境

转载 作者:行者123 更新时间:2023-12-03 00:51:06 25 4
gpt4 key购买 nike

我有这样定义的路线

@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class CartRoutingModule implements Resolve<ServiceCart> {
constructor(private service: CartService) {}

resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
const id = route.params['id'] ? route.params['id'] : null;
if (id) {
return this.service.find(id).pipe(map((Cart: HttpResponse<ServiceCart>) => Cart.body));
}

return of(new ServiceCart());
}
}

const routes: Routes = [
{
path: '', component: CartComponent,
children: [
{path: '', redirectTo: 'list', pathMatch: 'full'},
{path: 'list', component: ListComponent},
{path: 'new', component: CartEditComponent, resolve: {Cart: CartRoutingModule}}
]
}];

这里的问题是我永远无法让编译器通过,因为 routes 需要在 CartRoutingModule 之前定义才能在 @NgModule 中使用并且 CartRoutingModule 必须在 routes 之前定义,因为它是其中一个路由中的 resolve

因此编译器会抛出

TS2448: Block-scoped variable 'routes' used before its declaration.

TS2449: Class 'CartRoutingModule' used before its declaration.

那么这就像一个僵局

此时如何使用不同的注释或不同的方式在上下文中注入(inject)路由以使编译器满意?

最佳答案

您使用 NgModule 作为解析器有什么具体原因吗?听起来不对,因为 NgModels 的目的是配置,而 Resolver 提供数据。您可能想用 @Injectable 替换 @NgModel 装饰器并重命名它,使其有意义:

@Injectable({provideIn: 'root'}) // or wherever needed
export class CartResolve

关于javascript - 创建本地路由和解析器时的困境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53038200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com