2017-11-15 1 views
0

は私がサブモジュールのリストを取得ngOnInit主成分でルーティング構造親ルートが

path: '', 
    component: MainComponent, 
    children: [ 
     {path: 'employees', component: EmployeesComponent, canActivate: [AuthGuard], children: [ 
     {path: '', component: EmployeeListComponent, pathMatch: 'full', canActivate: [AuthGuard]}, 
     {path: ':id', component: EmployeeMainComponent, pathMatch: 'full', canActivate: [AuthGuard]} 
     ]} 
    ] 
    } 

を持ってレンダリングした後にのみ、子ルートをレンダリング

getSubmodules(id) { 
    this.universityAsideService.getSubmodules(id) 
     .subscribe((subModules) => { 
     this.subModules = subModules; 
     this.saveSubModules(subModules).subscribe((data) => this.goToModule(id)); 
     console.log('submodules in getSubmodules, UniversityAside Co'); 
     console.log(this.subModules); 
     }); 
    } 

    saveSubModules(subModules): Observable<any> { 
    return Observable.create((obs) => { 
     obs.next(localStorage.setItem('subModules', JSON.stringify({ 
     subModules: subModules 
     }))); 
    }); 
    } 

その後EmloyeesコンポーネントはのlocalStorageからサブモジュールのリストを取得し、ビューをレンダリング。 Bu訪問時の問題localhost:4200/#/employees/従業員コンポーネントは、MainComponentをlocalstorageに保存する前にlocalstorageからサブモジュールを取得しようとします。どうすればこの問題を解決できますか?ありがとう。

答えて

0

親コンポーネントの前に子コンポーネントがレンダリングされないように、AuthGuardを実装します。それをacheiveするために、私はAuthGuardに何を書くことができますThanks.So

path: '', 
    component: MainComponent, 
    canActivate: [AuthGuard], 
    children: [ 
     {path: 'employees', component: EmployeesComponent, canActivate: [AuthGuard], children: [ 
     {path: '', component: EmployeeListComponent, pathMatch: 'full', canActivate: [AuthGuard]}, 
     {path: ':id', component: EmployeeMainComponent, pathMatch: 'full', canActivate: [AuthGuard]} 
     ]} 
    ] 
    } 
+0

のようなルートを修正しますか?何も思いつきません。 AuthGuardでは、ユーザーが署名されているかどうかだけをチェックします。 –

+0

あなたのMainComponentがlocalstorageに保存されているかどうかをチェックしてください。 –

+0

と同じように実装できます。 –

関連する問題