首页 > 解决方案 > 如何从 axios 返回布尔值以禁用 vue 中的按钮

问题描述

我阅读了堆栈溢出中几乎所有已回答的问题来解决这个问题,但我仍然无法解决这个问题。我正在尝试调用一个函数来禁用我在 vue 中的按钮,并且该函数返回一个承诺而不是布尔值。我怎样才能在这里得到一个布尔值?

                        <v-btn
                        medium
                        color="white"
                        style="overflow: hidden"
                        @click="runValidationTaskNow(index)"
                        :loading="isRunTaskBtnDisabled(index)"
                        :disabled="isRunTaskBtnDisabled(index)"
                        >RUN NOW

……

在我的方法中,我调用了我在禁用和加载中提到的这个函数。

 async isRunTaskBtnDisabled(index) {
   const Status = await Util.checkFeatureEnabled("Validate "+this.validationTasks[index].productName);

  // Status.then(value => value);
   return Status;

并且 checkFeatureEnabled 来自下面给出的另一个文件

async checkFeatureEnabled(featureName){
const reqURL = process.env.VUE_APP_GUIDE_UTILITY_SERVICES_HOST + `/api/checkFeatureEnabled/` + featureName;
console.log("reqURL", reqURL);
return await Axios(reqURL, { method: "GET", withCredentials: true })
    .then((response) => {
      console.log("ui"+response.data);
    return response.data;
}).catch((error) => console.log(error));  

这里response.data是真还是假;我从我的数据库中得到了正确的结果。

标签: javascriptvue.jsaxios

解决方案


改变这部分

return await Axios(reqURL, { method: "GET", withCredentials: true })
    .then((response) => {
      console.log("ui"+response.data);
    return response.data;
}).catch((error) => console.log(error));  

对此:

try {
  const response = await Axios(reqURL, { method: 'GET', withCredentials: true })
  console.log('ui' + response.data)
  return response.data
} catch (e) {
  console.log(e)
  return something
}

推荐阅读