本文介绍了未捕获(承诺):类型错误:无法读取 null 的属性“组件"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Getting this error when trying to use nestet route in 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
                        }]
                    }
                ]

            }
        ]
    },

];

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

I want to make the following nested rules, each one using the variables to inform the nested components of each route:

/

/c/:concurso/

/c/:concurso/

/c/:concurso/:cargo/

/c/:concurso/:cargo/

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

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

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

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

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

On each level, I will need all the upper variables to make the correct querying of the related objects of the API.

感谢您的帮助!

推荐答案

如本文 (https://angular-2-training-book.rangle.io/handout/routing/child_routes.html) 处理子路由时的状态,就像定义路由器出口一样对于您的应用程序的根,您必须为您的父组件(在本例中为 ConcursoItemComponent.技术上还有 CargoItemComponent 和 DisciplinaItemComponent)定义一个路由器出口,因此您有 2 个选择.

As this article (https://angular-2-training-book.rangle.io/handout/routing/child_routes.html) states when dealing with child routes, just as you define a router-outlet for the root of your application, you must define a router-outlet for your parent component (in this case the ConcursoItemComponent. And technically also the CargoItemComponent & DisciplinaItemComponent) So you have 2 options.

  • 在 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
}

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

This way the router will always insert the component into the router-outlet at the root of the application.

这篇关于未捕获(承诺):类型错误:无法读取 null 的属性“组件"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 00:35