Angular2:授权路由后

时间:2017-04-21 10:27:45

标签: angular typescript angular2-routing

我有一个管理面板,我使用路由器移动到页面。管理面板将包含侧边栏(组件),标题(组件)和内容(组件)。在内容中我放了<router-outlet></router-outlet>。当我在页面中使用LoginComponent进行自动化时显示侧边栏和标题,因为内容中有<router-outlet></router-outlet>。我使用指令* ngIf作为可见性侧边栏(组件),标题(组件),但在我看来这不是最佳做法。我可以用其他变种吗? 我的文件夹结构: enter image description here

1 个答案:

答案 0 :(得分:0)

我使用“loadChildren”来解决此问题。这段代码在app.routing.module.ts

const routes: Routes = [
       { path: '', redirectTo: '/dashboard', pathMatch: 'full' },
       { path: 'dashboard', loadChildren: () => DashboardModule, canActivate: [AuthGuard]}, 
       { path: 'login',  component: LoginComponent}
 ]

此代码位于dashboard-routing.module.ts

const dashboardRoutes: Routes = [
{
 path: '',
 component: DashboardComponent,
 children : [
       { 
         path: '', 
         children:[
                   { path: 'user',  component: UserComponent, canActivate: [DashboardGuard] },
                   { path: 'user-crud',  component: UserCrudComponent, canActivate: [DashboardGuard] },
                   { path: 'user-crud/:id',  component: UserCrudComponent, canActivate: [DashboardGuard] }
                  ]
       },

此代码在app.component.ts中,在自动化之后,加载dashboard.module并在此组件的<router-outlet></router-outlet>中显示。

import { Component, OnInit} from '@angular/core';
  @Component({
       selector: 'app-root',
       template: '<router-outlet></router-outlet>'
  })

 export class AppComponent implements OnInit {
     constructor(){}
     ngOnInit() {}
 }

此代码在content.component.ts中,所有页面都显示在此组件中。“

import { Component, OnInit } from '@angular/core';
   @Component({
       selector: 'app-content',
       template: '<router-outlet></router-outlet>'
   })
export class ContentComponent implements OnInit {
   constructor() {}
   ngOnInit() {}
}