Multiple layouts in nested routes in Angular (2+)

前端 未结 2 1468
死守一世寂寞
死守一世寂寞 2020-12-29 16:16

I\'m creating a Dashboard like application. I\'d like to achieve the following layout plan in Angular (2+):

  • route - name - layout
  • / - home page - full
2条回答
  •  -上瘾入骨i
    2020-12-29 17:00

    You can solve your problem using child routes.

    See working demo at https://angular-multi-layout-example.stackblitz.io/ or edit at https://stackblitz.com/edit/angular-multi-layout-example

    Set your route like below

    const appRoutes: Routes = [
    
        //Site routes goes here 
        { 
            path: '', 
            component: SiteLayoutComponent,
            children: [
              { path: '', component: HomeComponent, pathMatch: 'full'},
              { path: 'about', component: AboutComponent }
            ]
        },
    
        // App routes goes here here
        { 
            path: '',
            component: AppLayoutComponent, 
            children: [
              { path: 'dashboard', component: DashboardComponent },
              { path: 'profile', component: ProfileComponent }
            ]
        },
    
        //no layout routes
        { path: 'login', component: LoginComponent},
        { path: 'register', component: RegisterComponent },
        // otherwise redirect to home
        { path: '**', redirectTo: '' }
    ];
    
    export const routing = RouterModule.forRoot(appRoutes);
    

    Recommended reference: http://www.tech-coder.com/2017/01/multiple-master-pages-in-angular2-and.html

提交回复
热议问题