javascript - Vue 条件轮询
问题描述
var page = new Vue({
el: '#content-page',
data: {
token: null
},
methods: {},
mounted: function () {
//get token object from API
}
});
令牌具有syncStatus
可以是inProgress
或的属性completed
。我想要一个条件轮询,它将继续调用 API,直到 syncStatus 完成值。
我可以做这样的事情:
var page = new Vue({
el: '#content-page',
data: {
token: null
},
methods: {
//Method-get-token
//In axios.then if syncStatus is inProgress call this method again
},
mounted: function () {
//get token object from API
//if syncStatus is inProgress call method-get-token
}
});
但我认为必须有一些更好的方法来做到这一点。
有什么建议么?
解决方案
假设你没有商店
您可以添加一个数据属性来保存syncStatus
then 在挂载函数中的值,有条件地调用 api 以继续检查要更改的值。syncStatus
然后添加一个观察者在值变化时做魔术
与此类似的东西
data() {
return {
syncStatus: 'notStarted' //you should think of having a default value to this
}
},
mounted() {
window.setInterval(()=>{
//call the api conditionally
if (syncStatus === 'inProgress'){
//call the api
}
},MilliSeconds)
},
watch: {
syncStatus: function(newValue) {
//do magic when the value changes
}
}
推荐阅读
- web-scraping - 网页抓取时“导入的内容为空”
- reactjs - React 中 WordPress 主题的未知位置
- azure - DevOps 管道触发多次触发来自原始管道的单个构建(有时)
- r - 将闪亮网络应用程序中 ggplot 图形的字体更改为谷歌的字体
- python - 如何删除列表中的重复项并将其在另一个列表中的相应值(按索引位置)更改为平均值?
- android - 构建 Android 包时出错“已安装的构建工具修订版 31.0.0 已损坏。”
- android - 'audio.content' (TYPE_BYTES) 的值无效,Base64 解码失败
- mysql - 查询使用 JOIN 获取客户列表,其中 sum() 为订单中花费的金额
- opencv - 如何在python中读取窗口屏幕?
- flutter - 用户向下滚动屏幕后如何清除图像缓存,如果向上滚动则重新加载?