【发布时间】:2017-12-01 01:45:30
【问题描述】:
在 Angular 4 中尝试使用嵌套路由时出现此错误:
ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'component' of null
TypeError: Cannot read property 'component' of null
at PreActivation.webpackJsonp.../../../router/@angular/router.es5.js.PreActivation.traverseRoutes (http://localhost:4200/vendor.bundle.js:77976:71)
at http://localhost:4200/vendor.bundle.js:77954:19
at Array.forEach (native)
at PreActivation.webpackJsonp.../../../router/@angular/router.es5.js.PreActivation.traverseChildRoutes (http://localhost:4200/vendor.bundle.js:77953:29)
at PreActivation.webpackJsonp.../../../router/@angular/router.es5.js.PreActivation.traverseRoutes (http://localhost:4200/vendor.bundle.js:77985:22)
这是我的路由代码:
const appRoutes: Routes = [
{
path: '',
component: HomeComponent
},
{
path: 'sobre',
component: SobreComponent
},
{
path: 'c/:concurso', component: ConcursoItemComponent
, children: [
{
path: ':cargo',
component: CargoItemComponent,
children: [
{
path: ':disc',
component: DisciplinaItemComponent,
children: [{
path: ':assunto',
component: AssuntoItemComponent
}]
}
]
}
]
},
];
我想做如下嵌套规则,每一个都使用变量来通知每条路由的嵌套组件:
/
/c/:concurso/
/c/:concurso/:cargo/
/c/:concurso/:cargo/:disc/
/c/:concurso/:cargo/:disc/:assunto
在每个级别上,我都需要所有上层变量来正确查询 API 的相关对象。
感谢您的帮助!
【问题讨论】:
-
您拉入路由文件的组件之一是否可能不正确?也许它是从错误的路径导入的?
-
好吧,我检查了导入路径,显然它们都是正确的。代码编译良好并且运行良好,直到 /c/:concurso/。错误发生在 /c/:concurso/:cargo/ 我还取出了组件构造函数中的所有 DI 并没有帮助...我的路由代码是否正确?
-
你的 ConcursoItemComponent 中有
标签吗? -
不,我在主应用程序组件中有一个插座。
-
当您分配给模型时来自后端的某些数据为空时。模型对象被破坏。
标签: javascript angular routes angular2-routing