升级到2.7.7后,发现切换路由的时候,loading组件会有一定几率不消失,查看官方仓库:
https://github.com/airyland/vux/commit/866a76302711e0198b69f3815196d0b51afef11c
发现组件有更新,添加了delay参数,可能存在bug。
相关代码:
Vue.http.interceptors.push((request, next) => {
Vue.$vux.loading.show({
text: 'Loading',
});
// 登录成功后将后台返回的TOKEN在本地存下来,每次请求从localStorage中拿到存储的TOKEN值
const TOKEN = localStorage.getItem('cw_token');
if (TOKEN) {
// 如果请求时TOKEN存在,就为每次请求的headers中设置好TOKEN,后台根据headers中的TOKEN判断是否放行
request.headers.set('token', TOKEN);
}
next((response) => {
Vue.$vux.loading.hide();
if (response.status == 200 && response.body.code == 10002) {
localStorage.removeItem('cw_token');
localStorage.removeItem('cw_user');
app.$vux.toast.show({
text: '会话已过期,请重新登录',
time: 1000,
})
setTimeout("location.href='/'", 1000);
}
return response;
});
});
bug已找到,在delay赋值为setTimeout前,没有手动clearTimeout,更改源码:
const loading = {
show (options = {}) {
// destroy watcher
watcher && watcher()
if (typeof options === 'string') {
$vm.text = options
} else if (typeof options === 'object') {
mergeOptions($vm, options)
}
if (typeof options === 'object' && options.onShow || options.onHide) {
watcher = $vm.$watch('show', (val) => {
val && options.onShow && options.onShow($vm)
val === false && options.onHide && options.onHide($vm)
})
}
clearTimeout(delayTime); // 避免在拦截器使用时,同时发起多个请求,delayTime重新赋值,macro队列引起的$vm.show = true滞后(后于hide()执行之后,所以loading一直存在)
delayTime = setTimeout(() => {
$vm.show = true
}, options.delay || 0)
},
hide () {
if (delayTime) {
clearTimeout(delayTime)
delayTime = null
}
$vm.show = false
}
}
PS: 官方api文档没有新增delay参数的说明,希望及时添加。
Fixed, thanks
@lichunqiang 还有一个小问题,官网的文档在更新delay参数说明后,loading的相关说明样式错乱了。
https://vux.li/#/zh-CN/components?id=loading
嗯,fix了
效率。
Most helpful comment
嗯,fix了