简体   繁体   English

从 angular observable ngrx/store 获取对象

[英]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.

相关问题 无法通过@ ngrx / effects @ 4.1.1和@ ngrx / store @ 4.1.1从角度4的可观察值中获取计数 - Unable to get the count from an observable in angular 4 with @ngrx/effects@4.1.1 and @ngrx/store@4.1.1 如何从ngrx存储中获取数据并将其更新到Observable变量中? - How to get and update data from the ngrx store into Observable variable? 如何在 TS 代码中从 NGRX Store 获取 Value 而不是 Observable - How to get Value instead of Observable from NGRX Store in TS code 带有 ngrx 存储的 Angular Rxjs 可观察链 - Angular Rxjs Observable Chain with ngrx store 是否可以从状态中获得常规值而不是 Observable angular Ngrx? - Is it possible to get a regular value from state and not as Observable angular Ngrx? 从 ngrx store pipe 选择器返回 Observable - Return Observable from ngrx store pipe selector 从ngrx /存储在Angular2中提取第一个对象 - pull first object from ngrx/store in Angular2 在 angular html 模板中显示来自ngrx 存储的单个 object - Displaying a single object from ngrx store in the angular html template Angular NgRx - 如何在我的模板 html 中查看商店中的对象? - Angular NgRx - How to view object from the store in my template html? 来自地图的角度 NgRX 返回值不可观察? - Angular NgRX return value from map is not observable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM