Angular 创建链接可观察订阅的正确方法
使用RxJS反应式重写此代码的更好方法是什么Angular 创建链接可观察订阅的正确方法,angular,typescript,rxjs,Angular,Typescript,Rxjs,使用RxJS反应式重写此代码的更好方法是什么 profileInformation; updateProfile(){ 让代币; 让profileId=1; this.userService.getAccessToken() .map((res)=>{ //因为我需要它多次 令牌=res; 返回res }) .switchMap(accessToken=>this.profleService.getContactId(accessToken,profileId)) .订阅((res)=>{ t
profileInformation;
updateProfile(){
让代币;
让profileId=1;
this.userService.getAccessToken()
.map((res)=>{
//因为我需要它多次
令牌=res;
返回res
})
.switchMap(accessToken=>this.profleService.getContactId(accessToken,profileId))
.订阅((res)=>{
this.profleService.updateprofile(令牌、this.profileInformation、res.account.id、res.address.id)
.订阅((数据)=>{
控制台日志(数据);
//处理数据
},(错误)=>{
//犯错时做某事
})
},(错误)=>{
//犯错时做某事
});
})
}
您可以再次执行switchMap
this.userService.getAccessToken()
.do(accessToken => token = accessToken)
.switchMap(accessToken => this.profleService.getContactIds(accessToken, profileId))
.switchMap(res => this.profleService.updateprofile(token, this.profileInformation, res.account.id, res.address.id))
.subscribe(
(data) => {
console.log(data)
}, (err) => {
});
你可以再做一次switchMap
this.userService.getAccessToken()
.do(accessToken => token = accessToken)
.switchMap(accessToken => this.profleService.getContactIds(accessToken, profileId))
.switchMap(res => this.profleService.updateprofile(token, this.profileInformation, res.account.id, res.address.id))
.subscribe(
(data) => {
console.log(data)
}, (err) => {
});
我在一秒钟内选择flatMap
的原因是switchMap
switchMap
传播发出值的第一个可观察成分observeable
。在原始代码中,您使用了一个嵌套的subscribe
,其下一个处理程序将为内部可观察的生成的每个值执行一次flatMap
(AKAmergeMap
),通过访问仍在取消测试时可能发出的所有值来保留原始语义
请注意,如果只有一个值发出,这将无关紧要,但从语义上讲,一个可观察对象可以发出许多值,我想保留这一点。我也认为这更清楚
如果您对传递给flatMap
(({account, address}) =>
this.profleService.updateprofile(token, this.profileInformation, account.id, address.id)
它被称为ES2015参数分解。它用于将属性直接从参数提取到局部变量中。我之所以假定帐户
和地址
是res
的属性,是因为它们是无条件访问的。这种语法在回调函数和一般投影函数中通常很有用
考虑:
interface Entity {
id: number;
}
declare const entities: Entity[];
function getById(entityId: number) {
return entities.find(({id}) => id === entityId);
}
我在一秒钟内选择flatMap
的原因是switchMap
switchMap
传播发出值的第一个可观察成分observeable
。在原始代码中,您使用了一个嵌套的subscribe
,其下一个处理程序将为内部可观察的生成的每个值执行一次flatMap
(AKAmergeMap
),通过访问仍在取消测试时可能发出的所有值来保留原始语义
请注意,如果只有一个值发出,这将无关紧要,但从语义上讲,一个可观察对象可以发出许多值,我想保留这一点。我也认为这更清楚
如果您对传递给flatMap
(({account, address}) =>
this.profleService.updateprofile(token, this.profileInformation, account.id, address.id)
它被称为ES2015参数分解。它用于将属性直接从参数提取到局部变量中。我之所以假定帐户
和地址
是res
的属性,是因为它们是无条件访问的。这种语法在回调函数和一般投影函数中通常很有用
考虑:
interface Entity {
id: number;
}
declare const entities: Entity[];
function getById(entityId: number) {
return entities.find(({id}) => id === entityId);
}
最后,我结合了两个答案中最好的一个,即朱利亚帕辛科娃和阿鲁安哈达德
profileInformation;
updateProfile(){
让代币;
让profileId=1;
this.userService.getAccessToken()
//对于多个使用令牌
.do(accessToken=>token=accessToken)
.switchMap(accessToken=>this.profleService.getContactId(accessToken,profileId))
.switchMap(res=>this.profleService.updateprofile(令牌、this.profileInformation、res.account.id、res.address.id))
.订阅((数据)=>{
控制台日志(数据);
//处理数据
},(错误)=>{
//犯错时做某事
})
}
我最终将两个答案中最好的结合起来@juliapassynkova和@aluanhaddad
profileInformation;
updateProfile(){
让代币;
让profileId=1;
this.userService.getAccessToken()
//对于多个使用令牌
.do(accessToken=>token=accessToken)
.switchMap(accessToken=>this.profleService.getContactId(accessToken,profileId))
.switchMap(res=>this.profleService.updateprofile(令牌、this.profileInformation、res.account.id、res.address.id))
.订阅((数据)=>{
控制台日志(数据);
//处理数据
},(错误)=>{
//犯错时做某事
})
}
我是否可以使用do()
而不是map()
来获得更好的外观^^?是的,“do”将起作用,但不会从“do”操作符返回任何内容。为什么我需要从do
操作符返回某些内容?在这种情况下,请使用map。do-对源可观测的每个发射执行副作用,但返回与源相同的可观测值。不要投射流。map中的return语句是标识投影,因此do
更干净。它也是更惯用的FWIW。我可以使用do()
而不是map()
来更好地查看^^?是的,“do”可以工作,但不从“do”操作符返回任何内容。为什么我需要从do
操作符返回某些内容?在这种情况下,使用map。do-对源可观测的每个发射执行副作用,但返回与源相同的可观测值。不要投射流。map中的return语句是标识投影,因此do
更干净。你能解释一下你在用平面地图做什么吗请参考我的答案你能解释一下这一部分吗请解释一下为什么你认为res会导致这个对象?如果你能在回答中告诉我更多关于这个问题的信息,那就太酷了