vue-directive进阶实现loading
<body>
<div id="root">
<div v-loading="isloading">{{data}}</div>
<button @click="update">更新</button>
</div>
<script>
Vue.directive("loading",{
update(el,binding,vnode){
console.log(binding)//binding的value值就是isloading
if(binding.value){
//让遮罩出现
const div=document.createElement("div")
div.setAttribute('id','loading')
div.innerHTML="加载中..."
div.style.position='absolute'
div.style.left=0
div.style.top=0
div.style.width='100%'
div.style.height='100%'
div.style.display='flex'
div.style.justifyContent='center'
div.style.alignItems='center'
div.style.color='white'
div.style.background='rgba(0,0,0,.7)'
document.body.append(div)
}else{
document.body.removeChild(document.getElementById('loading'))
}
}
})
new Vue({
el:"#root",
data:function(){
return{
data:'',
isloading:false
}
},
methods:{
update(){
this.isloading=true
setTimeout(()=>{
//遮罩消失
this.isLoading=false
//数据要出来
this.data='这是后端请求的数据'
},3000)
}
}
})
</script>
</body>