【问题标题】:function not returning expected value typescript函数不返回预期值打字稿
【发布时间】:2019-10-12 00:30:26
【问题描述】:

我有一个通过 api 调用检查库存的函数,我将它调用到另一个函数中。但这总是在执行 apimethod.post 之前返回 true 似乎 apimethod.post 在 if 条件之后进行评估。 有什么帮助吗?

public IsStockAvailable = (): any => {
  return this.apimethod.post(url, body, true).subscribe((response: any) => {
    if (response.RCode == AppConstant.ResponseStatus.StockError) {
      return false;
    }
    if (response.RCode == AppConstant.ResponseStatus.Success) {
      return true;
    }
  })
}

AddItem(){
if(!IsStockAvailable ){
    return;
  }
   // other part of code
}

【问题讨论】:

标签: angular typescript


【解决方案1】:

你必须在 if 语句中调用你的函数

if(!IsStockAvailable() ){
    return;
}

否则,您只需将函数定义传递给 if 语句,它就是真的!

【讨论】:

    猜你喜欢
    • 2022-01-12
    • 2019-08-09
    • 1970-01-01
    • 1970-01-01
    • 2021-04-10
    • 2017-11-25
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    相关资源
    最近更新 更多