我從NGRX /商店獲得保留列表,數據正常進入,但在頁面重新加載時消失。我必須改變路線才能找回它。角度2丟失頁面重新加載的數據
ngOnInit() {
this.store.select(state => state.authState.profile).subscribe(profile => {
this.profile = profile;
if (this.profile) {
this.userId = this.profile.$key;
}
});
if (this.userId) {
this.reservations$ = this.reservationService.loadUserReservations(this.userId);
}
}
ngOnChanges() {
if (this.userId) {
this.reservations$ =
this.reservationService.loadUserReservations(this.userId);
}
}
不確定這是否足夠說明問題的代碼。
在我的身份驗證服務
this.auth$.authState.subscribe(user => {
if (user) {
const userData = user;
const userRef = db.object("users/" + userData.uid);
const dataToSet = {
displayName: userData.providerData[0].displayName,
email: userData.providerData[0].email,
photoURL: userData.providerData[0].photoURL,
providerId: userData.providerData[0].providerId,
registeredAt: new Date()
};
userRef.take(1).subscribe((user) => {
if (user.$exists()) {
// console.log('user exists', user);
this.store.dispatch(this.authActions.updateUserInfo(user));
} else {
// console.log('user does not exists');
userRef.set(dataToSet)
.then((result) => {
// console.log(result)
})
.catch((error) => {
// console.log(error);
});
}
});
// console.log('hello user', user);
this.store.dispatch(this.authActions.loginSuccess(user));
} else {
this.store.dispatch(this.authActions.logOutUser());
}
});
}
我使用火力地堡和訂閱的authState變化。 當用戶登錄時我發送一個動作來更新商店,我只使用authState.profile來過濾用戶的預訂,所有的工作都很好。我在ngOnInit()上設置可觀察預訂$。
'頁面重新加載'你的意思是整個頁面重新加載?就像當你按下瀏覽器F5時? –
是的,一個完整的重新加載。角色CLI在我保存更改時重新加載頁面,這就是我注意到的方式。 –
好的。所以你刷新頁面,但留在相同的組件(路線)。這裏似乎有幾個問題。但首先,你能分享一下代碼,你實際上填充了authState'部分的狀態嗎?以及觸發那些填充該狀態的代碼的東西?你是在做一些登錄組件或? –