可以在Angular中subscribe()函数内的属性分配对象
我定义了类型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[]; }
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须将
this.prsnls
分配给属性。不知道如何键入操作,请尝试以下(或类似的内容):
You have to assign
this.prsnls
to a property.Without knowing how the action is typed, try the following (or something similar):