gpt4 book ai didi

javascript - Angular2 v3 组件路由器 : TypeError: Cannot read property 'split' of undefined

转载 作者:搜寻专家 更新时间:2023-10-30 22:04:52 24 4
gpt4 key购买 nike

我最近将我的 angular2 应用程序从 RC 路由器切换到了 v3 路由器。我遇到了错误

TypeError: Cannot read property 'split' of undefined
at match`.

在路由器上启用跟踪以获取调试输出后,错误源似乎来自路由器:

NavigationError {id: 2, url: "/my/workspace/1252407935628215305/projects", error: TypeError: Cannot read property 'split' of undefined
at match - common_router_providers.js:28 NavigationError {id: 2, url: "/my/workspace/1252407935628215305/projects", error: TypeError: Cannot read property 'split' of undefined
at match (http://localhost:8080/vendor.6a9d…}error: TypeError: Cannot read property 'split' of undefined
at match (http://localhost:8080/vendor.6a9dd9122cbbc98aa93a.js:74100:22)
at matchPathsWithParamsAgainstRoute (http://localhost:8080/vendor.6a9dd9122cbbc98aa93a.js:74073:19)
at expandPathsWithParamsAgainstRoute (http://localhost:8080/vendor.6a9dd9122cbbc98aa93a.js:74038:17)
at expandPathsWithParams (http://localhost:8080/vendor.6a9dd9122cbbc98aa93a.js:74020:21)
at matchPathsWithParamsAgainstRoute (http://localhost:8080/vendor.6a9dd9122cbbc98aa93a.js:74085:23)
at expandPathsWithParamsAgainstRoute (http://localhost:8080/vendor.6a9dd9122cbbc98aa93a.js:74038:17)
at expandPathsWithParams (http://localhost:8080/vendor.6a9dd9122cbbc98aa93a.js:74020:21)
at expandSegment (http://localhost:8080/vendor.6a9dd9122cbbc98aa93a.js:74010:17)
at http://localhost:8080/vendor.6a9dd9122cbbc98aa93a.js:74014:84
at http://localhost:8080/vendor.6a9dd9122cbbc98aa93a.js:74367:41message: "Cannot read property 'split' of undefined"stack: (...)get stack: stack()set stack: stack()__proto__: Errorid: 2url: "/my/workspace/1252407935628215305/projects"__proto__: Object

这是我的路由器配置设置:

export const routes: RouterConfig = [
...MyAppRoutes,
{ path: 'login', component: LoginComponent},
{ path: 'signup', component: SignUpComponent},
{ path: 'join', component: JoinComponent},
{ path: 'version', component: VersionComponent},
{ path: '', redirectTo: 'login', terminal: true},
];

export const APP_ROUTER_PROVIDERS = [
provideRouter(routes, {enableTracing: true})
];

我的应用程序路径:

export const MyAppRoutes: RouterConfig = [
{ path: 'my',
component: MyAppComponent,
children: [
{path: 'dashboard', component: DashboardComponent},
{path: 'profile', component: EditProfileComponent},
{path: 'password', component: ChangePasswordComponent},
{path: '', redirectTo: 'dashboard', terminal: true},
...WorkspaceRoutes,
]
},
];

工作区路线

export const WorkspaceRoutes: RouterConfig = [
{path: 'workspace/:id', component: WorkspaceRootComponent},
{children: [
{path: 'projects', component: WorkspaceDashboardComponent},
{path: 'newproject', component: ProjectNewComponent},
{path: '', redirectTo: 'projects', terminal: true},
]}
];

路由在前两个级别(应用程序路由和 MyAppRoutes)运行良好。但是尝试导航到任何路线,例如 /my/workspace/1234/projects 失败并出现上述错误。同样的设置适用于 Angular2 beta 和 v2 路由器。

最佳答案

我明白了这是怎么回事。以防其他人遇到类似问题,本例中的问题出在 WorkspaceRoutes 中。

export const WorkspaceRoutes: RouterConfig = [
{path: 'workspace/:id', component: WorkspaceRootComponent},
{children: [ // this should not be the start of a new object
{path: 'projects', component: WorkspaceDashboardComponent},
{path: 'newproject', component: ProjectNewComponent},
{path: '', redirectTo: 'projects', terminal: true},
]}
];

应该是

export const WorkspaceRoutes: RouterConfig = [
{path: 'workspace/:id',
component: WorkspaceRootComponent,
children: [
{path: 'projects', component: WorkspaceDashboardComponent},
{path: 'newproject', component: ProjectNewComponent},
{path: '', redirectTo: 'projects', terminal: true},
]}
];

WorkspaceRootComponent 的子项不在该项下,但在同一级别。

关于javascript - Angular2 v3 组件路由器 : TypeError: Cannot read property 'split' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38063098/

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