<html>
<style>
@keyframes rotate
{
0% {transform:rotateX(0)}
50% {transform:rotateX(180deg)}
100% {transform:rotateX(0deg)}
}
.loading{
animation:rotate 2s infinite;
}
</style>
<body>
<div id="app">
{{msg}}
<button @click="test">click</button>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.js"></script>
<script>
let MyPlugin = {}
MyPlugin.install = function(Vue,options){
let defaultLoading = {
data:function(){
return {
isShow:false,
msg:""
}
},
methods:{
show(msg){
this.msg = msg||"toast消息"
this.isShow = true
},
close(){
this.isShow = false
}
},
template:`<div class="loading" v-show="isShow" style="width:400px;height:200px;line-height:200px;text-align:center;border:1px solid red;">
{{msg}} default loading
</div>`
}
let Loading = Vue.extend(defaultLoading)
let loading = new Loading({
el:document.createElement('div'),
})
document.body.appendChild(loading.$el)
Vue.prototype.$loading = {
show:loading.show.bind(loading),
close:loading.close.bind(loading)
}
}
let LoadingComponent = {
data:function(){
return {
}
},
props:['isShow','msg'],
template:`<div style="width:400px;height:200px;line-height:200px;text-align:center;border:1px solid red;">
{{msg}}
</div>`
}
Vue.use(MyPlugin,{loadingComponent:LoadingComponent})
new Vue({
el:"#app",
data:{
isShow:false,
msg:"xxx"
},
methods:{
test(){
this.isShow = !this.isShow
this.isShow?this.$loading.show():this.$loading.close()
}
}
})
</script>
</body>
</html>