Angular2 v3组件路由器:TypeError:无法读取属性' split'未定义的

时间:2016-06-27 20:56:52

标签: javascript angularjs typescript angular angular2-routing

我最近将我的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})
];

MyAppRoutes:

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,
    ]
  },
];

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路由器。

1 个答案:

答案 0 :(得分:7)

我弄清楚发生了什么事。如果其他人遇到类似的问题,这种情况下的问题是在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的子项不在该项下,但在同一级别上。