vue项目中,设置页面局部loading加载效果(element)v-loading
    		       		warning:
    		            这篇文章距离上次修改已过420天,其中的内容可能已经有所变动。
    		        
        		                
                在Vue项目中,可以使用Element UI库中的v-loading指令来实现页面局部加载效果。首先确保已经安装了Element UI,然后可以按照以下步骤进行操作:
- 在Vue项目中创建一个自定义指令(如果还没有的话)。
- 使用Element UI的Loading服务。
- 应用这个指令到你想要显示加载效果的元素上。
以下是一个简单的自定义指令的示例代码:
// 在你的Vue组件或者Vue实例中
import { Loading } from 'element-ui';
 
export default {
  // ...
  directives: {
    'loading': {
      bind(el, binding, vnode) {
        // 当绑定了该指令的元素首次挂载到DOM上时
        if (binding.value) {
          // 创建一个Loading实例
          const loading = Loading.service({
            lock: true,
            text: '加载中...',
            background: 'rgba(0, 0, 0, 0.7)'
          });
          // 将loading实例存储在元素的自定义数据属性上
          el.loading = loading;
        }
      },
      unbind(el, binding) {
        // 当元素被卸载时,关闭loading
        if (el.loading) {
          el.loading.close();
        }
      }
    }
  }
  // ...
};在模板中使用这个自定义指令:
<template>
  <div v-loading="isLoading">
    <!-- 这里是页面的其他内容 -->
  </div>
</template>
 
<script>
export default {
  data() {
    return {
      isLoading: false
    };
  },
  // ...
};
</script>在这个例子中,isLoading是一个响应式数据,当它为true时,会在包含它的div上显示加载效果。当你不再需要显示加载效果时,将isLoading设置为false即可。
评论已关闭