How to set featured module default landing component via router in angular 2

假如想象 提交于 2019-12-13 03:34:00

问题


I have two modules (clientModule, AdminModule), the admin module is lazy loaded.

On page load, the client module loads.

Client module routes

const _routes: Routes = [
  {
    path: ':id', component: appComponent, children: [
      { path: 'page1', component: page1Component},
      { path: 'admin', loadChildren:  './admin/admin.module#AdminModule' }
    ]
  }
];

Admin module routes

const _routes: Routes = [
  {
    path: '', component: AdminComponent, children: [
      { path: 'adminPage1', component: adminPage1Component},
      { path: 'adminPage2', component: adminPage2Component},

    ]
  }

];

Problems

  1. The requirement is to navigate to 'adminPage1Component' component on Admin module load.

回答1:


I was able to solve it, if anyone having the same issue, the below way worked.

Admin module route configuration

const _routes: Routes = [
  {
    path: '', component: AdminComponent, children: [
      { path: ' ', component: adminPage1Component},
      { path: 'adminPage1', component: adminPage1Component},
      { path: 'adminPage2', component: adminPage2Component},

    ]
  }
];

by adding a empty path ( { path: ' ', component: adminPage1Component} ) inside the children array loaded the component by default.

further, if you an have element which need to add in a css class when the component is loaded. ex:

 <li routerLink="adminPage1" routerLinkActive="active">
          Admin Page 1
 </li> 

you can do the below to support routerLinkActive

const _routes: Routes = [
  {
    path: '', component: AdminComponent, children: [
      { path: ' ', component: adminPage1Component, redirectTo: 'adminPage1',  pathMatch: 'full'},
      { path: 'adminPage1', component: adminPage1Component},
      { path: 'adminPage2', component: adminPage2Component},

    ]
  }
];

I basically added "redirectTo: 'adminPage1', pathMatch: 'full'" to the empty path.



来源:https://stackoverflow.com/questions/52123600/how-to-set-featured-module-default-landing-component-via-router-in-angular-2

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