简体   繁体   English

无法在 Angular 的 subscribe() 函数中分配对象的属性

[英]Can't assign object's properties inside the subscribe() function in Angular

I defined the object persnel of type prsnlStateInterface and trying to assign the properties by subscription to an observable fails.我定义了persnel类型的对象prsnlStateInterface并尝试通过订阅 observable 来分配属性失败。 Here is the persnels definition:这是persnels定义:

 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 :这是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})) .问题是 Visual Studio 在尝试使用this.store.dispatch(loadPrsnls({this.persnels}))调度 NgRx 存储中的数据时抱怨{this.persnels} 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:这是一个屏幕截图: 在此处输入图像描述

What I don't understand is that I've not defined persnels of type any, but of type prsnlStateInterface .我不明白的是我没有定义任何类型的prsnlStateInterface persnels 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.此外,当我用console.log('persnl', this.persnels)替换this.store.dispatch(loadPrsnls({this.persnels}))项目编译但在控制台中 prsnels 属性未定义。

What am I missing here and how to get it right?我在这里缺少什么以及如何正确处理?

You have to assign this.prsnls to a property.您必须将this.prsnls分配给一个属性。 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}))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM