Uncaught (in promise): TypeError: Cannot read property 'component' of null

Uncaught (in promise): TypeError: Cannot read property 'component' of null

在 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)

这是我的路由代码:

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/:货物/

/c/:concurso/:cargo/:disc/

/c/:concurso/:cargo/:disc/:assunto

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

感谢您的帮助!

正如本文 (https://angular-2-training-book.rangle.io/handout/routing/child_routes.html) 所述,在处理子路由时,就像您为应用程序的根定义路由器出口一样,您必须为父组件定义路由器出口(在在这种情况下,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
}

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

只是想我会添加评论,以使那些出于与我相同的原因而偶然发现此问题的人受益。如果您的模板使用条件渲染,并且异步满足这些条件,则路由器出口不能在条件标记内,因为框架可能会在条件满足之前尝试渲染标记。例如:

<div *ngIf="someAsyncCall()">
   <header>{{some result from the async call}}</header>
   <router-outlet></router-outlet>
</div>

可能 失败取决于异步调用完成的速度。在条件渲染中只包含标记的静态部分总是更安全。如:

<div *ngIf="someAsyncCall()">
    <header>{{some result from the async call}}</header>
</div>
<router-outlet></router-outlet>

我通过将整个页面包装在 "busy indicator" 指令中得到了一点,这几乎可以保证路由器插座不会一直可用。事后看来似乎很明显,但是......

如果有人有兴趣使用子路由结构。你可以遵循这个模型。我在 ngx-breadcrumbs 中找到了这个。

const myRoutes : Route[] = {
  {
    path: '',
    component: HomeComponent        
  },
  {
    path: 'about',
    component: AboutComponent        
  },
  {
    path: 'person',
    children: [
      {
          path: '',
          component: PersonListComponent
      },
      {
          path: ':id',
          component: PersonDetailComponent              
      } 
    ]
  },    
  {
    path: 'folder',        
    children: [
      {
      path: '',
      component: FolderComponent
      },
      {
        path: ':id',
        component: FolderComponent            
      }
    ]
  }
};