我从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'部分的状态吗?以及触发那些填充该状态的代码的东西?你是在做一些登录组件或? –