对父子路由使用一个组件

Using one component for parent and child routes

我创建了一个延迟加载的模块,当用户重定向到路由时它必须加载一个组件但是我收到如下所述的错误:

Error: Uncaught (in promise): Error: Cannot find primary outlet to load 'XYZComponent'

这是我的模块。

import { XYZComponent } from './xyz-confirm'

const XYZConfirmRoute: Route[] = [
  { 
    path: '', component: XYZComponent ,
    children: [
        { path: 'test', component: XYZComponent}
    ]
  }
];

@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    RouterModule.forChild(XYZConfirmRoute)
  ],
  declarations: [XYZComponent],
  exports: [RouterModule],
  schemas: [ CUSTOM_ELEMENTS_SCHEMA ]  
})
export class XYZConfirmModule { }

正如您在上面看到的,我想为父路由和子路由加载 XYZComponent。有什么办法可以做到这一点?

你就不能简单地做到这一点吗

  {
    path: 'test', component: XYZComponent
  },{
    path: '', component: XYZComponent
  }

注意 你必须像我一样在顶部定义狭窄的路径 test is on顶部然后 ''