【问题标题】:A variable call returns value 'undefined' outside of a subscriber function变量调用在订阅者函数之外返回值“未定义”
【发布时间】:2020-06-28 18:08:52
【问题描述】:

之前有人问过与此类似的问题,并收到了很多回复,我可能听起来有些重复,但这只是迫切需要第三只眼睛。我正在以角度为订阅者函数内的变量分配一个值,但是在订阅函数之外调用该变量时返回“未定义”。这就是正在做的事情:

    this.invoiceService.buildDataForDiscountGraph(this.usrId, this.currentYear)
      .subscribe( response => {
        this.discountsGraphData += response;
        //this.discountGraphData is of type 'string'
    });
This.discountsGraphData

在订阅者内调用时返回所需的值,但在订阅者之外,在另一个函数中,甚至我的模板中,它都是一个空字符串。

这是 buildDataForDiscountGraph() 函数的外观:

buildDataForDiscountGraph(u,y){
    return this.getUserInvoices(u)
      .pipe(
        map(response  => {
          if(Object.keys(response).length > 0)
          {
            let jan = 0, feb = 0, mar = 0, apr = 0;
            let may = 0, june = 0, july = 0, aug = 0;
            let sep = 0, oct = 0, nov = 0, dec = 0;

            for(let x = 0; x < Object.keys(response).length; x++) {
              let dateSplit = response[x]['datecreated'].split('-');
              let year = dateSplit[0];
              let month = dateSplit[1];

              if(y == year){
                if(month == '01')jan += response[x]['discount'];
                if(month == '02')feb += response[x]['discount'];
                if(month == '03')mar += response[x]['discount'];
                if(month == '04')apr += response[x]['discount'];
                if(month == '05')may += response[x]['discount'];
                if(month == '06')june += response[x]['discount'];
                if(month == '07')july += response[x]['discount'];
                if(month == '08')aug += response[x]['discount'];
                if(month == '09')sep += response[x]['discount'];
                if(month == '10')oct += response[x]['discount'];
                if(month == '11')nov += response[x]['discount'];
                if(month == '12')dec += response[x]['discount'];
              }
            }

            return [
              jan, feb, mar, apr, may, june, july, aug,
              sep, oct, nov, dec
            ].join();
          }  
      })
    )
  }

我不得不将代码移动到我的服务中,以试图找出导致它的原因。 Service 函数 getUserInvoices() 像这样调用端点;

/**
   * Get Invoices
   *
   * @param userId
   * @param Limit
   * @param Offset
   */
  getUserInvoices(usrId, limit?, offset?){
    return this.http.get(this.apiEndPoint + usrId + '/invoices?limit=' + limit + '&offset=' + offset)
      .pipe(
        map( data =>
          {
            return (data ? data : []);
          }
        )
      )
  }

【问题讨论】:

  • 你能为此提供一个堆栈闪电战吗?
  • 您在从 buildDataForDiscountGraph 函数获取值之前访问 discountsGraphData 变量
  • 请问访问 discountGraphData 变量的理想方法是什么?

标签: javascript angular typescript angular-components angular9


【解决方案1】:

您可以使用函数在订阅者之外设置 this.discountGraphData

this.invoiceService.buildDataForDiscountGraph(this.usrId, this.currentYear)
  .subscribe( response => {
      this.setDate(response);
    //this.discountsGraphData += response;
    //this.discountGraphData is of type 'string'
});

setDate(data){
    this.discountsGraphData = data;
    console.log(this.discountsGraphData);  --> you will get value here.
}

【讨论】:

  • 谢谢!以上有效,但无效:setDate(data){ this.discountsGraphData = data; } console.log(this.discountsGraphData); --&gt; you will get value here.
  • 我不能从另一个函数调用 this.discountsGraphData 吗?那是在按照您的建议进行设置之后?
  • 如果你想在页面加载之前获取数据,然后使用解析器,你会得到数据,一旦你设置了 this.discountsGraphData 的值,你就可以在该控制器的任何函数中使用该值
猜你喜欢
  • 2020-08-12
  • 2020-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-22
  • 2022-07-29
  • 1970-01-01
相关资源
最近更新 更多