未捕获(在承诺中):TypeError:无法读取null的属性"component"

lmi*_*ael 21 javascript routes angular2-routing angular

尝试在Angular 4中使用nestet路由时出现此错误:

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)
Run Code Online (Sandbox Code Playgroud)

这是我的路由代码:

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

            }
        ]
    },

];
Run Code Online (Sandbox Code Playgroud)

我想制作以下嵌套规则,每个规则使用变量来通知每个路由的嵌套组件:

/

/ C /:concurso /

/ C /:concurso /:货物/

/ C /:concurso /:货物/:盘/

/ C /:concurso /:货物/:盘/:ASSUNTO

在每个级别上,我将需要所有上层变量来正确查询API的相关对象.

谢谢你的帮助!

LLa*_*Lai 27

正如本文(https://angular-2-training-book.rangle.io/handout/routing/child_routes.html)所述,当处理子路由时,就像为应用程序的根定义路由器出口一样,你必须为你的父组件定义一个router-outlet(在这种情况下是ConcursoItemComponent.技术上也是CargoItemComponent和DisciplinaItemComponent)所以你有2个选项.

  • 在ConcursoItemComponent中定义路由器出口.这样,当用户访问c /:concurso /:cargo时,路由器将知道加载子组件(CargoItemComponent)的位置
  • 不要使用子路由,而是将所有路由设置在顶级路由器级别(应用程序的根目录)

{
    path: 'c/:concurso,
    component: ConcursoItemComponent
},
{
    path: 'c/:concurso/:cargo,
    component: CargoComponent
},
{
    path: 'c/:concurso/:cargo/:disc,
    component: DisciplinaItemComponent
},
{
    path: 'c/:concurso/:cargo/:disc/:assunto,
    component: AssuntoItemComponent
}
Run Code Online (Sandbox Code Playgroud)

这样,路由器将始终将组件插入应用程序根目录的路由器出口