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
)可以得到预期的结果。