【问题标题】:Angular - Return from subscribeAngular - 从订阅返回
【发布时间】:2020-05-17 16:40:41
【问题描述】:

我正在尝试使用 subscribe 向 this.voucherDetails 返回值,但它似乎不起作用。

以下是对具有订阅功能的getVoucherDetails函数的请求。

voucherIDs: string[];
voucherDetails: Promise<any>[];

this.voucherIDs = ['JV-2005-50','JV-2005-40','JV-2005-30'];
this.voucherDetails = this.voucherIDs
.map(id => this.getVoucherDetails(id));
Promise.all(this.voucherDetails)
      .then(() => this.printService.onDataReady());

以下是订阅用户服务的函数,它从数据库中获取数据。

getVoucherDetails(voucherid) {
    var splitvoucher = voucherid.split('-');
    var vouchertype = splitvoucher[0];
    var vouchermy = splitvoucher[1];
    var voucherno = splitvoucher[2];
    var vouchernotopass = vouchermy+"-"+voucherno;

    var voucherdate;
    var enteries;

    this.userService.getVoucher(vouchernotopass,vouchertype).subscribe(
        res => {
          voucherdate = res['voucherdate'];
          enteries = res['enteries'];
          return { "vouchertype":vouchertype, "vouchernumber": vouchernotopass, "voucherdate": voucherdate, "enteries":enteries};   
        }
    );       
}

但它返回未定义。在浏览了几篇帖子后,我了解到您无法从 .subscribe 中返回。但我无法理解如何解决这个问题。

【问题讨论】:

    标签: angular typescript


    【解决方案1】:

    您正在使用 Observable 异步工作,因此您需要使用回调,例如使用 Promise。你应该在你的方法中返回一个 Observable(或 Promise)并让调用者订阅它:

    getVoucherDetails(voucherid) {
        var splitvoucher = voucherid.split('-');
        var vouchertype = splitvoucher[0];
        var vouchermy = splitvoucher[1];
        var voucherno = splitvoucher[2];
        var vouchernotopass = vouchermy+"-"+voucherno;
    
        var voucherdate;
        var enteries;
    
        return this.userService.getVoucher(vouchernotopass,vouchertype).pipe(
            map(res => {
              voucherdate = res['voucherdate'];
              enteries = res['enteries'];
              return { "vouchertype":vouchertype, "vouchernumber": vouchernotopass, "voucherdate": voucherdate, "enteries":enteries};   
            })
        );       
    }
    

    像这样使用这个方法:

    getVoucherDetails(id).subscribe(v => {
      console.log(v.vouchertype);
      console.log(v.vouchernumber);
      console.log(v.voucherdate);
    });
    

    在你的情况下:

    forkJoin( // forkJoin waits for Observables to complete and then combine last values they emitted.
       this.voucherIDs.map(id => this.getVoucherDetails(id) // array of observable
    ).subscribe(responses => {
       console.log(responses);
       this.voucherDetails = responses;
    });
    

    【讨论】:

    • 感谢您的友好回复,并为我的知识有限道歉。按照您建议的方式使用 getVoucherDetails 时,voucherDetails 的值为 [Subscriber,Subscriber,Subscriber]。如何获取 getVoucherDetails 返回并存储在凭证详细信息中的实际值
    • 我刚刚编辑了我的答案添加如何在你的情况下使用它
    • 我尝试在对凭证详细信息的响应中添加推送,它说不可分配给 Promise 类型的参数。尝试设置 forkJoin = couponDetails 也没有用。 :(
    • 不,您应该在订阅中分配voucherDetails。如果你想使用 Promise + Promise.all:this.voucherDetails = this.voucherIDs.map(id =&gt; this.getVoucherDetails(id).toPromise());
    • 感谢您的耐心等待和所有帮助。最后一件事如何在 html 中显示这个 [Object Promise]?我试过 {{ couponDetails[0].vouchertype }} 并返回 [Object Promise]
    猜你喜欢
    • 2016-07-20
    • 1970-01-01
    • 2021-07-16
    • 2017-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-09
    • 1970-01-01
    相关资源
    最近更新 更多