Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vue.js 如何根据Axios调用的结果退出while循环?_Vue.js_While Loop_Axios - Fatal编程技术网

Vue.js 如何根据Axios调用的结果退出while循环?

Vue.js 如何根据Axios调用的结果退出while循环?,vue.js,while-loop,axios,Vue.js,While Loop,Axios,为了确保用户输入的单词存在,我需要不断提示(whileloop)一个单词,直到word API找到该单词 我的问题是:如何根据Axios调用结果退出而循环 下面是到目前为止我的代码 const wordApiBaseUrl = 'https://www.dictionaryapi.com/api/v1/references/sd4/xml' while (true) { const wordToGuess = prompt('Enter a word:').toLowerCase() c

为了确保用户输入的单词存在,我需要不断提示(
while
loop)一个单词,直到word API找到该单词

我的问题是:如何根据Axios调用结果退出
循环

下面是到目前为止我的代码

const wordApiBaseUrl = 'https://www.dictionaryapi.com/api/v1/references/sd4/xml'
while (true) {
  const wordToGuess = prompt('Enter a word:').toLowerCase()
  const endPointUrl = `${wordApiBaseUrl}/${wordToGuess}?key=${wordApiKey}`
  this.axios.get(endPointUrl).then(res => {
    if (res.data.includes('def')) {
      break
    }
  })
}
试试这个:

const-wordApiBaseUrl='1〕https://www.dictionaryapi.com/api/v1/references/sd4/xml'
const vm=this;//{
如果(!res.data.includes('def')){
dispatcher.execute();
}
})
}
}
dispatcher.execute();

您可以在我们的承诺中使用递归,而不是使用
while
循环或
async/await
。如果结果不满意,请重新运行AJAX调用。

谢谢,但是获得一些
\u this.execute并不是一个函数
错误。这是一个
Vue
应用程序,我需要用
这个调用Axios。Axios
抱歉,我使用了一个匿名函数
这个
在anonymous JS lambdas中不存在。现在就尝试一下,您可以在
调度程序之外捕获它。执行
,我将更新我的答案以向您演示如何执行。编辑:更新为使用
this.axios
,在
dispatcher.execute
之外使用别名
this
。谢谢,仍然收到相同的错误
Uncaught(in promise)TypeError:_this.execute不是一个函数
。我不明白他为什么。。我已经更新了答案,并捕获了AJAX调用之外的
调度程序。试试看。在本地运行(不使用
vm.axios
)可以得到预期的结果。