1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| import Vue from "vue"; import LoadingComponent from "./loading.vue";
const LoadingContructor = Vue.extend(LoadingComponent);
Vue.directive("loading", {
bind(el, binding) { const instance = new LoadingContructor({ el: document.createElement("div"), data: {} }); el.appendChild(instance.$el); el.instance = instance; Vue.nextTick(() => { el.instance.visible = binding.value; }); },
update(el, binding) { if (binding.oldValue !== binding.value) { el.instance.visible = binding.value; } },
unbind(el) { const mask = el.instance.$el; if (mask.parentNode) { mask.parentNode.removeChild(mask); } el.instance.$destroy(); el.instance = undefined; } });
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <template> <van-loading v-if="visible" /> </template> <script> import { Loading } from "vant"; export default { data() { return { visible: false }; }, components: { [Loading.name]: Loading } }; </script>
|