2017-04-21 183 views
0

我有一個管理面板,我使用路由器來移動到頁面。管理面板將由邊欄(組件),標題(組件)和內容(組件)組成。在內容我把<router-outlet></router-outlet>。當我在頁面中使用LoginComponent進行自動化時顯示側欄和標題,因爲<router-outlet></router-outlet>裏面的內容。我使用指令* ngIf作爲可見性側欄(組件),標題(組件),但在我看來這不是最佳實踐。我可以用其他變體嗎? 我的文件夾結構: enter image description hereAngular2:授權路由後

+0

如果可能,你能澄清一下嗎?你想做什麼 ? 如果我理解正確,如果您沒有授權,您正在使用'* ngIf'來顯示/隱藏組件,但是您想要做比這更好的事情? 你應該使用路由器'Guard'。看看這篇文章的回答:http://stackoverflow.com/questions/39183404/angular-2-route-restriction-with-angularfire-2-auth/39186038#39186038 –

+0

@AlexBeugnet我使用一個路由器衛隊,但它''沒有解決我的問題 – aimprogman

回答

0

我使用「loadChildren」來解決此問題。這段代碼在app.routing.module.ts

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

這段代碼在儀表板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,autorization,裝載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() {} 
}