【发布时间】:2016-07-07 04:17:23
【问题描述】:
我想在身份验证后重定向到我的主组件。重定向成功,但是 home 组件没有渲染。如果我将 this.router.navigate 移到 subscribe 块之外,它将正确呈现。
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);
}
}
【问题讨论】:
-
什么是
authServiceauthenticationStream`? -
authService.authenticationStream()返回一个指示身份验证状态的布尔值的 Observable 流 -
请添加显示它的代码