可以在Angular中subscribe()函数内的属性分配对象

发布于 2025-02-01 19:38:10 字数 2910 浏览 2 评论 0原文

我定义了类型prsnlstateinterface的对象persnel,并尝试通过订阅可观察到的失败来分配属性。这是persnels定义:

 persnels : prsnlStateInterface = {
    id :'',
    firstname:'',
    lastname : '',
    fullname :'',
    profession : '',
    dob : '',
    nationality: '',
    createdDate: '',
    sex: '',
    gender :'',
    phone :0,
    adresse : '',
    prsnlRoleIds : [0],
    total :0,
    selectedUserId :0,
    ids :[] ,
    entities :{},
  }

这是prsnlstateinterface

export interface prsnlStateInterface {
    "id": String,
    "firstname": String,
    "lastname": String,
    "fullname": String,
    "profession": String,
    "dob": String,
    "nationality": String,
    "createdDate": String,
    "sex": String,
    "gender": String,
    "phone": number,
    "adresse": String,
    "prsnlRoleIds": [number],
    "total": number,
    "selectedUserId":number,
    "ids":[], 
    "entities":{}
}

这就是我订阅承诺的方式:

return this.persnlService.getPersnl(data)
    .pipe(
      map((response) => response)
    ).subscribe((obj) => {
    for (const prsnl of Object.values(obj)[2]) {
      console.log('ids', prsnl.id);
      this.persnels = {
        id : prsnl.id,
        firstname: prsnl.id,
        lastname : prsnl.firstname,
        fullname: prsnl.fullname,
        profession: prsnl.function,
        dob: prsnl.dob,
        nationality: prsnl.nationality,
        createdDate: prsnl.createdDate,
        sex: prsnl.sex,
        gender: prsnl.gender,
        phone: prsnl.phone,
        adresse: prsnl.adresse,
        prsnlRoleIds: prsnl.prsnlRoleIds,
        total: 10,
        selectedUserId:0,
        ids:[] ,
        entities:{},
      };
    }; this.store.dispatch(loadPrsnls({this.persnels}));
  })

问题是Visual Studio在抱怨{ this.persnels}尝试使用this.store.dispatch(loadprsnls({this.persnels}))this.store.dispatch中的数据进行分配时。它说:

Argument of type '{ this: any; }' is not assignable to parameter of
type '{ prsnls: prsnlStateInterface[]; }'.
   Object literal may only specify known properties, and 'this' does not exist in type '{ prsnls: prsnlStateInterface[]; }

这是一个屏幕截图:

我不明白的是,我没有定义persnels类型的 type prsnlstateinterface。此外,当我替换this.store.dispatch(loadprsnls({this.persnels}))) by console.log('persnl',this.persnels) project compiles compiles compiles但是在控制台中,PRSNELS属性是不确定的。

我在这里错过了什么?

I defined the object persnel of type prsnlStateInterface and trying to assign the properties by subscription to an observable fails. Here is the persnels definition:

 persnels : prsnlStateInterface = {
    id :'',
    firstname:'',
    lastname : '',
    fullname :'',
    profession : '',
    dob : '',
    nationality: '',
    createdDate: '',
    sex: '',
    gender :'',
    phone :0,
    adresse : '',
    prsnlRoleIds : [0],
    total :0,
    selectedUserId :0,
    ids :[] ,
    entities :{},
  }

Here is the the prsnlStateInterface:

export interface prsnlStateInterface {
    "id": String,
    "firstname": String,
    "lastname": String,
    "fullname": String,
    "profession": String,
    "dob": String,
    "nationality": String,
    "createdDate": String,
    "sex": String,
    "gender": String,
    "phone": number,
    "adresse": String,
    "prsnlRoleIds": [number],
    "total": number,
    "selectedUserId":number,
    "ids":[], 
    "entities":{}
}

And this is how I subscribe to the promise:

return this.persnlService.getPersnl(data)
    .pipe(
      map((response) => response)
    ).subscribe((obj) => {
    for (const prsnl of Object.values(obj)[2]) {
      console.log('ids', prsnl.id);
      this.persnels = {
        id : prsnl.id,
        firstname: prsnl.id,
        lastname : prsnl.firstname,
        fullname: prsnl.fullname,
        profession: prsnl.function,
        dob: prsnl.dob,
        nationality: prsnl.nationality,
        createdDate: prsnl.createdDate,
        sex: prsnl.sex,
        gender: prsnl.gender,
        phone: prsnl.phone,
        adresse: prsnl.adresse,
        prsnlRoleIds: prsnl.prsnlRoleIds,
        total: 10,
        selectedUserId:0,
        ids:[] ,
        entities:{},
      };
    }; this.store.dispatch(loadPrsnls({this.persnels}));
  })

The problem is that Visual Studio is complaining about the {this.persnels} when trying to dispatch the data inside NgRx store with this.store.dispatch(loadPrsnls({this.persnels})). It says:

Argument of type '{ this: any; }' is not assignable to parameter of
type '{ prsnls: prsnlStateInterface[]; }'.
   Object literal may only specify known properties, and 'this' does not exist in type '{ prsnls: prsnlStateInterface[]; }

Here is a screenshot:
enter image description here

What I don't understand is that I've not defined persnels of type any, but of type prsnlStateInterface. Moreover, when I replaced this.store.dispatch(loadPrsnls({this.persnels})) by console.log('persnl', this.persnels) the project compiles but in the console the prsnels properties are undefined.

What am I missing here and how to get it right?

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

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

发布评论

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

评论(1

旧夏天 2025-02-08 19:38:11

您必须将this.prsnls分配给属性。
不知道如何键入操作,请尝试以下(或类似的内容):

//                               

You have to assign this.prsnls to a property.
Without knowing how the action is typed, try the following (or something similar):

//                               ???? assign the value to the `persnels` property
this.store.dispatch(loadPrsnls({ persnels: this.persnels}))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文