gpt4 book ai didi

Angular 无法使用不同数量的 child 重新附加 ActivatedRouteSnapshot

转载 作者:太空狗 更新时间:2023-10-29 19:31:42 25 4
gpt4 key购买 nike

这是我在 nativescript-angular 项目中的路由:

const routes: Routes = [

{
path: "",
redirectTo: "/tabs/default",
pathMatch: "full"
},
{
path: "tabs",
loadChildren: "~/app/modules/tabs/tabs.module#TabsModule"
},
{
path: "login",
loadChildren: "~/app/modules/login/login.module#LoginModule"
},
{
path: "register",
loadChildren: "~/app/modules/register/register.module#RegisterModule"
}

];

@NgModule({
imports: [NativeScriptRouterModule.forRoot(routes, {
preloadingStrategy: PreloadAllModules
})],
exports: [NativeScriptRouterModule]
})

导致错误的场景:首先应用程序以选项卡路由开始,然后我转到登录页面,然后我去注册然后再次转到选项卡页面(并清除历史记录)。之后,如果我再次进入登录页面并返回到上一个(标签页),则会发生错误。

错误:

JS: Error: Cannot reattach ActivatedRouteSnapshot with a different number of children
JS: at setFutureSnapshotsOfActivatedRoutes (file:///data/data/org.nativescript.tns57/files/app/tns_modules/@angular/router/bundles/router.umd.js:1950:19) [angular]
JS: at setFutureSnapshotsOfActivatedRoutes (file:///data/data/org.nativescript.tns57/files/app/tns_modules/@angular/router/bundles/router.umd.js:1954:13) [angular]
JS: at createNode (file:///data/data/org.nativescript.tns57/files/app/tns_modules/@angular/router/bundles/router.umd.js:1935:17) [angular]
JS: at file:///data/data/org.nativescript.tns57/files/app/tns_modules/@angular/router/bundles/router.umd.js:1975:20 [angular]
JS: at Array.map (<anonymous>) [angular]
JS: at createOrReuseChildren (file:///data/data/org.nativescript.tns57/files/app/tns_modules/@angular/router/bundles/router.umd.js:1958:30) [angular]
JS: at createNode (file:///data/data/org...

有什么想法吗?

最佳答案

尝试改变第一条路线

{
path: "",
redirectTo: "tabs",
pathMatch: "full"
}

然后像这样设置你的TabsModule

const routes: Routes = [    
{
path: "",
redirectTo: "default",
pathMatch: "full"
},
{
path: "default",
component: YourDefaultComponent
}
...
];

更新:如果您遇到同样的错误,您可以使用AttachDetachReuseStrategy

import { ActivatedRouteSnapshot, RouteReuseStrategy, DetachedRouteHandle } from '@angular/router';

interface RouteStorageObject {
snapshot: ActivatedRouteSnapshot;
handle: DetachedRouteHandle;
}

export class CustomReuseStrategy implements RouteReuseStrategy {


handlers: { [key: string]: RouteStorageObject } = {};

retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle {
if (!route.routeConfig) return null;
if (route.routeConfig.loadChildren) return null;
return this.handlers[route.routeConfig.path];
}

.....

}

然后我让 shouldDetach 函数也检查 loadChildren 以防止 RouteReuseStrategy 保存错误 ActivatedRouteSnapshot:

shouldDetach(route: ActivatedRouteSnapshot): boolean {
if (!route.routeConfig || route.routeConfig.loadChildren) {
return false;
}
return true;
}

当然,修改你的AppModule

@NgModule({
imports: [...],
providers: [
{provide: RouteReuseStrategy, useClass: CustomReuseStrategy}
],
....
)}
export class AppModule {
}

关于Angular 无法使用不同数量的 child 重新附加 ActivatedRouteSnapshot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56218581/

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