可以使用从NGRX Store收集的数据创建一个类型接口的对象

发布于 2025-01-30 03:09:01 字数 1080 浏览 3 评论 0原文

我有这个接口:

export interface AuthStateInterface {
    isSubmitting: boolean,
    X_HI_CODE: string,
    token: string,
    userId : number,
    roleId: number   
}

我想创建一个接口类型的对象,并使用其他类中的NGRX存储中收集的一些数据使用方法initializeValues()将对象创建为Bellow:

  initializeValues(): void {
    const data: AuthStateInterface = {
      isSubmitting: false,
      X_HI_CODE: this.store.pipe(select(xhicodeSelector)),
      token: this.store.pipe(select(tokenSelector)),
      userId : this.store.pipe(select(userIdSelector)),
      roleId: this.store.pipe(select( roleIdSelector)),
    };
    console.log('data', data)
  }

但是Visual Studio错误如您所见。屏幕截图:

当我悬停在红色警报上时,这就是它所说的:

“在此处输入图像说明”

如果有人可以帮助我解决这个问题,我会很棒。

I have this interface:

export interface AuthStateInterface {
    isSubmitting: boolean,
    X_HI_CODE: string,
    token: string,
    userId : number,
    roleId: number   
}

And I want to create an object of this interface type with some data collected from the ngrx store within another class with the method initializeValues() that creates the objects as bellow:

  initializeValues(): void {
    const data: AuthStateInterface = {
      isSubmitting: false,
      X_HI_CODE: this.store.pipe(select(xhicodeSelector)),
      token: this.store.pipe(select(tokenSelector)),
      userId : this.store.pipe(select(userIdSelector)),
      roleId: this.store.pipe(select( roleIdSelector)),
    };
    console.log('data', data)
  }

But Visual Studio errors out as you can see on the screenshot bellow:
enter image description here

When I hover on the red alert, this is what it says:

enter image description here

I will be greatfull if anyone can help me fix this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情痴 2025-02-06 03:09:01

一般来说,我误会了几个角度或打字稿的概念。正如@mikeone在评论中所说的那样,NGRX选择器返回可观察到的,我不得不订阅并将其分配给我之前声明的变量。所以这就是我所做的:

1-我初始化了变量,而不是观察到:

isSubmitting!: boolean
  token!: string
  roleId!: [number]
  userId!: string

2-我使用selector钩3选择了来自ngrx商店的数据

ngOnInit(): void {
    this.initializeValues()
    this.getAllStaff()
  }
  
  initializeValues(): void {

    this.store
    .pipe(select(isSubmittingSelector), map((isSubmitting) => isSubmitting))
    .subscribe(value => this.isSubmitting = value);
    this.store

    .pipe(select(tokenSelector), map((token) => token))
    .subscribe(value => this.token = value);

    this.store
    .pipe(select(roleIdSelector), map((roleId) => roleId))
    .subscribe(value => this.roleId = value);

    this.store
    .pipe(select(userIdSelector), map((userId) => userId))
    .subscribe(value => this.userId = value);
  }

3-然后我使用这些值创建传递给下一个承诺的数据

   getAllPersnlValues() {
    const data: AuthStateInterface = {
      isSubmitting: this.isSubmitting,
      X_HI_CODE: this.HI_CODE,
      token: this.token,
      userId : this.userId,
      roleIds: this.roleId,
    };
    console.log('data', data);
   
    return this.persnlService.getPersnl(data)
    .pipe(
      map((response) => response)
    );
  }

应该是做到这一点的一种较短的方法,在我的ReactJs背景下,我还有更多关于RXJ的知识,然后才吹嘘更简单的技术。但是这种方法使这项任务完成了。

There were several concepts of angular or typescript in general that I was misunderstanding. As @MikeOne said in the comments, Ngrx selectors return Observables, and I had to subscribe and assign them to the variables I've declared previously. So this is what I did:

1- I initialized the variables, not observables:

isSubmitting!: boolean
  token!: string
  roleId!: [number]
  userId!: string

2- I selected the data from the ngrx-store with the selector hook

ngOnInit(): void {
    this.initializeValues()
    this.getAllStaff()
  }
  
  initializeValues(): void {

    this.store
    .pipe(select(isSubmittingSelector), map((isSubmitting) => isSubmitting))
    .subscribe(value => this.isSubmitting = value);
    this.store

    .pipe(select(tokenSelector), map((token) => token))
    .subscribe(value => this.token = value);

    this.store
    .pipe(select(roleIdSelector), map((roleId) => roleId))
    .subscribe(value => this.roleId = value);

    this.store
    .pipe(select(userIdSelector), map((userId) => userId))
    .subscribe(value => this.userId = value);
  }

3- Then I used the values to create the data passed to next promise

   getAllPersnlValues() {
    const data: AuthStateInterface = {
      isSubmitting: this.isSubmitting,
      X_HI_CODE: this.HI_CODE,
      token: this.token,
      userId : this.userId,
      roleIds: this.roleId,
    };
    console.log('data', data);
   
    return this.persnlService.getPersnl(data)
    .pipe(
      map((response) => response)
    );
  }

There should be a shorter way to do it, and with my Reactjs background I had much more to learn about rxjs before boasting on simpler technics. But this approach made this task completed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文