前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >vant增加全局遮罩层

vant增加全局遮罩层

作者头像
用户6362579
发布2020-05-04 15:07:57
2.8K0
发布2020-05-04 15:07:57
举报
文章被收录于专栏:小神仙小神仙

vant默认提供的加载遮罩太水了,也可能是我太水了不会用,所以找大神写了一个,我抄过来了


增加遮罩控件

在任意位置增加如下两个文件,注意loadingIndex.js引用loading.vue时路径要修改成你的:

  1. 模板文件:loading.vue
代码语言:javascript
复制
<template>
  <div>
    <van-overlay
      :show="isShow"
      :custom-style="{
        background: 'rgb(255, 255, 255, 0.6)',
        display: 'flex',
        justifyContent: 'center',
        paddingTop: '100px'
      }"
    >
      <van-loading size="24px" color="#4994df">
        <span style="color:#4994df">{{ title || '加载中···' }}</span>
      </van-loading>
    </van-overlay>
  </div>
</template>
  1. js文件:loadingIndex.js
代码语言:javascript
复制
import vue from 'vue'
import loadingComponent from './loading.vue'

const LoadingConstructor = vue.extend(loadingComponent)

let toastDom, el;

function showLoading({ title, type, duration = 2000 }) {
  if (!el && !toastDom) {
    el = document.createElement('div');
    toastDom = new LoadingConstructor({
      el,
      data() {
        return {
          isShow: true, // 是否显示
          title // 文本内容
        };
      }
    });
    // 添加节点
    document.body.appendChild(toastDom.$el);
  } else {
    toastDom.isShow = true;
  }
}

function cancelLoading() {
  if (toastDom) {
    toastDom.isShow = false;
  }
}

// 全局注册
function registryToast() {
  vue.prototype.$showLoading = showLoading;
  vue.prototype.$cancelLoading = cancelLoading;
}

export default registryToast;

在vue中引用控件

在你引用vue的地方增加如下代码,注意路径改为你的路径

代码语言:javascript
复制
import Vue from "vue";
import loadingIndex from "./loading/loadingIndex";
Vue.use(loadingIndex);

使用遮罩

然后你就可以像下面一样使用遮罩了:

代码语言:javascript
复制
showloading() {
    var title = "加载中···";
    this.$showLoading({
      title: title
    });
  }

  hideloading() {
    this.$cancelLoading();
  }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-04-24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 增加遮罩控件
  • 在vue中引用控件
  • 使用遮罩
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档