我想重定向到我的家庭組件認證後。重定向成功,但主頁組件不呈現。如果我將this.router.navigate移動到訂閱塊外部,它將正確呈現。路由器導航重定向後身份驗證不呈現組件
export class LoginComponent implements AfterViewInit {
constructor(private router: Router,
private authService: AuthenticationService) {
this.authService.authenticationStream().subscribe(isAuthenticated => {
if (isAuthenticated) {
this.router.navigate(['/home']);
}
});
}
}
在Chrome瀏覽器開發工具,我可以看到所有的家庭成分的模板除了一個ngFor塊:
<md-grid-tile *ngFor="let tile of CONFIG.tiles">
{{ tile }}
</md-grid-tile>
我可以驗證CONFIG.tiles填充。
爲什麼在導航後不會呈現ngFor塊,特別是在Observable訂閱中導航調用?
編輯:添加了AuthenticationService代碼:
export class AuthenticationService {
constructor(private http: HttpService,
private store: Store<AppStore>) {
}
authenticate(): void {
let uri = 'http://uri.com'
this.http.post(uri).subscribe(() => {
// Dispatch event to ngrx store if user is successfully authenticated
this.store.dispatch({type: AUTHENTICATED});
});
}
authenticationStream(): Observable<boolean> {
// Emits events from dispatch calls
return <Observable<boolean>> this.store.select(AUTHENTICATION);
}
}
什麼是'authService'authenticationStream'? –
'authService.authenticationStream()'返回一個表示驗證狀態的布爾值的Observable流 –
請添加顯示它的代碼 –