且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

找不到要加载"LocalizationListComponent"的主要出口

更新时间:2023-11-17 18:13:34

我刚刚在GitHub上遇到了一个问题(

I just came across an issue on GitHub (https://github.com/angular/angular/issues/10686) where it is explained that this is an intentional behavior. Every parent that has children (and it seems the same goes for loadChildren) must have it's own outlet in which to load the children.

路由器文档中似乎没有任何文档.

It seems that this was not documented anywhere in the Router documentation.

更新: 当您在模块的路由器中声明children时,每个父级都必须拥有自己的<router-outlet></router-outlet>才能加载此子级.如何实现这一点取决于需求.对于我的解决方案,我创建了一个仅包含<router-outlet></router-outlet>的空组件,并将路由定义如下:

UPDATE: When you declare children in a module's router, each parent must have it's own <router-outlet></router-outlet> in order to load this children. How to implement this depends on the needs. For my solution I created an empty component that just contains <router-outlet></router-outlet> and defined the route as following:

import { Routes, RouterModule } from "@angular/router"
import { SomeParentComponent } from "./app/modules/SomeModule/SomeParentComponent"
import { SomeChildComponent1 } from "./app/modules/SomeModule/SomeChild1Component"
import { SomeChildComponent2 } from "./app/modules/SomeModule/SomeChild2Component"

const routes: Routes = [
    path: "",
    component: SomeParentComponent,
    children: [
        {
            path: "",
            component: SomeChild1Component
        },
        {
            path: ":id",
            component: SomeChild2Component
        }
    ]
]

export const someModuleRoutes = RouterModule.forChild(routes);

这应该可以解决问题,因为主要组件路由将调用loadChildren指向SomeParentComponent.

This should do the trick as the main component route will call loadChildren pointing at SomeParentComponent.