[英]Get object from angular observable ngrx/store
I have a problem getting an object from the @ngrx/store.我在从 @ngrx/store 获取对象时遇到问题。 This code worked fine for a long time, but now it seems to be broken and I don't know why.这段代码运行了很长时间,但现在似乎坏了,我不知道为什么。
The object to store has this structure:要存储的对象具有以下结构:
export class UserInfo {
user: string;
token: string;
permissions: string[];
}
This function saves my object to the store:此函数将我的对象保存到商店:
saveUserInfo(userInfo: UserInfo) {
this.store.dispatch(new UserInfoActions.Save(userInfo));
}
Now when I try to get the object back from the store like this:现在,当我尝试像这样从商店取回对象时:
userInfoStore$: Observable<UserInfo>;
constructor(private store: Store<AppState>) {
this.userInfoStore$ = this.store.select('userInfo').subscribe(user => {
console.log(user)
});
}
The desired output is something like所需的输出类似于
{user: "username", token: "token", permissions: []}
But the output is:但输出是:
{0: "{", 1: "\"", 2: "u", 3: "s", 4: "e", 5: "r", 6: "\"", 7: ":", 8: "\"", 9: "u", 10: "s", 11: "e", 12: "r", 13: "n", 14: "a", 15: "m", ...}
What is going on here?这里发生了什么? Has something changed how observables
work?有什么改变了observables
工作方式吗?
Angular v9.1.0 @ngrx/store v9.0.0 Angular v9.1.0 @ngrx/store v9.0.0
The reason behind this is your reducer is returning stringified userInfo
.这背后的原因是您的减速器正在返回字符串化的userInfo
。 check the userInfo
that you are receiving from API and the reducer where you are returning the userInfo from.检查您从 API 接收的userInfo
和您从中返回 userInfo 的减速器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.