Angular2 Router v3, navigate to different level: TypeError: Cannot read property '_routeConfig' of null

Angular2 Router v3, navigate to different level: TypeError: Cannot read property '_routeConfig' of null

我正在使用新的路由器 v3.0.0-alpha.3。想象一下我的路线是这样的:

                              App
                              /\
                             /  \
                       Settings  Main
                          / \        
                         /   \      
                       User  Account

我想从用户组件的角度导航到主组件。如果我导航到设置级别中的任何组件,它工作正常,但当我尝试导航到其他级别时,我收到错误消息:

EXCEPTION: Error: Uncaught (in promise): TypeError: Cannot read property '_routeConfig' of null

我使用 routerLink 指令导航:

[routerLink]="['/main']"           <- doesn't work
[routerLink]="['/settings']"       <- works
[routerLink]="['/settings/user']"  <- works

这似乎是一个全球性问题。我只能在同一级别内导航。知道如何绕过它吗?

我的路线传递给 bootstrap:

export const routes: RouterConfig = [
    ...MainRoutes,
    ...SettingsRoutes
];

export const APP_ROUTER_PROVIDERS = [
    provideRouter(routes)
];

main.routes.ts:

export const MainRoutes: RouterConfig = [{
    path: '/main',
    component: SettingsComponent
}];

settings.routes.ts:

export const SettingsRoutes: RouterConfig = [{
    path: '/settings',
    component: SettingsComponent,
    children: [{
        path: '/user',
        component: UserSettingsComponent,
        index: true
    }]
}];

编辑:

由于评论中给出的 link 不再有效,我会建议临时修复。该错误仍然存​​在于 alpha.7 版本的路由器中,但将被修复 here

问题在router.js文件,函数GuardChecks.prototype.runCanDeactivate第309行(路由器v.alpha.7):

var canDeactivate = curr._routeConfig ? curr._routeConfig.canDeactivate : null;

改为:

var canDeactivate = curr && curr._routeConfig ? curr._routeConfig.canDeactivate : null;

请记住,这应该被视为临时修复,不适合自动化生产环境!

更新:已在 alpha.8

中修复

在发布新的 @angular/router 之前,您需要按照此处所述手动更新源代码:

https://github.com/angular/angular/issues/9480#issuecomment-227845866