Angular 2/4/5 - Load child component to main router-outlet

时光毁灭记忆、已成空白 提交于 2019-12-23 02:21:39

问题


I am working with angular 5 currently. I have came across the problem with my router configuration and its presentation of component content accordingly. As below is my router configuration.

  const appRoutes: Routes = [
    { path: "", pathMatch: "full", redirectTo: "home" },
    {
      path: "home", component: HomeComponent
    },
    {
      path: "applications", component: ApplicationsComponent,
      children: [{
        path: ":applicationId/privacysettingsforapplications", component: PrivacysettingsforapplicationsComponent,
        children: [
          { path: "", pathMatch: "full", redirectTo: "services" },
          {
            path: "services", component: PrivacysettingsforapplicationservicesComponent
          },
          {
            path: "data", component: PrivacysettingsforapplicationdataComponent
          }]
      }]
    },
    {
      path: "devices", component: DevicesComponent,
    },
  ];

We have one main <router-outlet></router-outlet> in which angular loads all content of its parent component. Here ApplicationsComponent has one children PrivacysettingsforapplicationsComponent which in turn has two children(PrivacysettingsforapplicationservicesComponent and PrivacysettingsforapplicationdataComponent ie: service and data).

I want to check that is there any way to load(PrivacysettingsforapplicationsComponent) children on main <router-outlet> instead of its immediate parent <router-outlet> ie. ApplicationComponent's. One way I have checked is to put child component as peer to parent component but then my breadcrumb module does not identify who is the real parent of that component PrivacysettingsforapplicationsComponent.


回答1:


path: "applications/:applicationId/privacysettingsforapplications"

Replace this with following path

path: ":applicationId/privacysettingsforapplications"




回答2:


You need to provide the full path instead of just router path. e.g. provide path line applications/privacysettingsforapplications/services.

You need to provide the correct path as the above is just an example.

With this the service template will load on parent most router outlet.

Hope this help.



来源:https://stackoverflow.com/questions/47976360/angular-2-4-5-load-child-component-to-main-router-outlet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!