私は、ログインが完了したら、別のページにルーティングする、非常に単純で直感的な振る舞いであると思うものを実装しようとしています。しかし、一部のデータはコンポーネントの1つで更新されていないため、Angular 2の変更検出と関係があると思われます。角2 +角膜炎2ログイン - 変化を検出しない
これは私のコードです:
ログインコンポーネント - ページモジュールの一部
constructor(private _af: AngularFire,
private _router: Router,
private _zone: NgZone) {
}
onLoginSubmit() {
this._af.auth.login({
email: this.auth.email,
password: this.auth.pass
}).then(success => {
this._zone.run(() => {
this._router.navigate(['/']);
});
})
}
ダッシュボードコンポーネント - ページモジュールの一部 - ルート '/'
ngOnInit() {
this._stateService.currentPageName = 'Dashboard';
}
Topbarコンポーネント - UIモジュールの一部
ngOnInit() {
this._router.events.subscribe(() => {
this.pageName = this._stateService.currentPageName;
});
}
Topbarテンプレート
<!-- This is not updated when I'm routing from the login page,
but it shows fine when I'm reloading the page or switching to any other routes -->
<a class="navbar-brand">{{ pageName }}</a>
この種の状況に対処する必要があります。これは、初期化時に利用できる変更だけでなく、後で発生する変更を検出するためです。 – Muirik
'ngOnChanges(){ this.pageName = this._stateService.currentPageName;を追加しようとしました。 } ' をトップバーコンポーネントに追加しても、何も起こりません。これは正しい場所ですか? –
また、ngOnChangesライフサイクルフックは、アプリケーション全体の値ではなく、入力値の変化に反応します。 –