Angular无法使用其他数量的子代重新附加ActivatedRouteSnapshot

时间:2019-05-20 10:02:38

标签: angular angular-router 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...

有什么主意吗?

3 个答案:

答案 0 :(得分:0)

尝试更改第一条路线

{
    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 {
}

答案 1 :(得分:0)

尝试将版本更新至nativescript-angular至7.2.4。您posted使用的回购链接使用的版本为7.2.3,其中包含路由方面的一些问题。

https://github.com/NativeScript/nativescript-angular/pull/1740

答案 2 :(得分:0)

您尝试过传递relativeTo附加功能吗?奇怪的是,这对我有用,我不知道如何以及为什么。

this.routerExtension.back({relativeTo: this.activatedRoute});

喜欢这个。