2017-04-21 9 views
0

私は管理パネルを持っていますが、ページを移動するためのルータを使用しています。管理パネルは、サイドバー(コンポーネント)、ヘッダ(コンポーネント)、コンテンツ(コンポーネント)で構成されます。コンテンツでは、私は<router-outlet></router-outlet>を入れた。私は、コンテンツ内に<router-outlet></router-outlet>があるので、ページ内のサイドバーとヘッダーを表示するには、自動化にLoginComponentを使用します。私は可視性サイドバー(Component)、ヘッダ(Component)に* 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私はルータGuardを使用していますが、私の問題を解決しない – aimprogman

答えて

0

私はこの問題を解決するために "loadChildren"を使用します。 autorization、ローディングdashboard.module後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でこのコードをこのコードで、このコードは、このコンポーネントの<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() {} 
} 
関連する問題