首页 > 其他分享 >Vue3手写一个全局命令式loading组件

Vue3手写一个全局命令式loading组件

时间:2024-07-01 23:27:06浏览次数:3  
标签:loading const app mountNode vue 命令式 Vue3 import

实现效果:

vue文件中,打开全局loading... 2s后关闭

全局命令式loading,效果展示完,直接咱就是上代码 

注册:  
<!-- src/components/myLoading/index.vue -->
<template>
  <!-- 添加name属性,以添加样式
  Transition 主要做一个淡入淡出的 -->
  <Transition name="zhLoading">
    <div v-if="show_toast" class="zh-loading-mark" :style="{ background: bgColor }">
      <div class="zh-loading-body">
        <div class="msg">
          {{ msg }}
        </div>
      </div>
    </div>
  </Transition>
</template>
<script setup>
const props = defineProps({
  show_toast: true,
  msg: "",
  // toast_type: "success",
  duration: 1500
});
const bgColor = 'rgba(43, 41, 39, 0.276)'
</script>
<style scoped lang="scss">
.zh-loading-mark {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;

  .zh-loading-body {
    margin: auto;
    margin-top: 35vh;
    text-align: center;
  }

  /* transition标签样式添加 */
  .zhLoading-enter,
  .zhLoading-leave-to {
    opacity: 0;
  }

  .zhLoading-enter-active,
  .zhLoading-leave-active {
    transition: opacity 0.6s;
  }
}
</style>
// src/components/myLoading/index.js
// 引入vue
import { createVNode, render } from "vue";
// 引入loading组件
import MyLoading from './index.vue';

let mountNode = null;
const showLoading = (options) => {
  const { msg, duration = 2000 } = options;
  //确保只存在一个弹框,如果前一个弹窗还在,就移除
  if (mountNode) {
    document.body.removeChild(mountNode);
    mountNode = null;
  }
  //将options参数传入,并将MyLoading组件转换成虚拟DOM,并赋值给app
  const app = createVNode(MyLoading, {
    msg,
    duration,
    show_toast: true,
  });
  //创建定时器,duration时间后将mountNode移除
  let timer = setTimeout(() => {
    if (mountNode) {
      document.body.removeChild(mountNode);
      mountNode = null;
    }
    clearTimeout(timer);
  }, duration);
  //创建一个空的div
  mountNode = document.createElement('div');
  //render函数的作用就是将Notice组件的虚拟DOM转换成真实DOM并插入到mountNode元素里
  render(app, mountNode);
  //然后把转换成真实DOM的MyLoading组件插入到body里
  document.body.appendChild(mountNode);
}

export default showLoading;
// main.js
import { createApp } from 'vue';
import App from './App.vue';
import showLoading from './components/myLoading/index.js'

const app = createApp(App);

// 绑定全局方法
app.config.globalProperties.$showLoading = showLoading

app.use(router);
app.mount('#app');
使用:
<!-- about.vue -->
<template>
  <el-button @click="onBtn">点击触发</el-button>
</template>
<script setup>
import { getCurrentInstance } from 'vue'

const { proxy } = getCurrentInstance()

function onBtn() {
  proxy.$showLoading({
    msg: '努力加载鸭...',
    show_toast: true
  })
}

</script>

标签:loading,const,app,mountNode,vue,命令式,Vue3,import
From: https://blog.csdn.net/qq_54548545/article/details/139939666

相关文章

  • ts vue3 getCurrentInstance 使用,$refs 调用方式
    代码示例可以通过ref变量实现绑定$ref,或者getCurrentInstance来获取$refs/***$ref使用方式,变量名和组件的属性ref值一致*/consthChildRef=ref()console.log(hChildRef,"hChildRef")constinstance=getCurrentInstance()//constself=(instanceasComponen......
  • vue3一些高阶用法
    1.ref和reactive首先,让我们从Vue3最基础也是最常用的两个方法开始:ref 和 reactive。它们是响应式基础。refref 用于定义一个响应式的数据对象。它适用于单个基本类型或对象的场景。<scriptsetup>import{ref}from'vue'constcount=ref(0)functionincr......
  • 自定义vue3 hooks
    文章目录hooks目录结构demohooks当页面内有很多的功能,js代码太多,不好维护,可以每个功能都有写一个js或者ts,这样的话,代码易读,并且容易维护,组合式setup写法与此结合......
  • 一个项目学习Vue3---Vue模板方法
     内容资源下载:关注公众号(资小库),下载相关资源分析下面一段代码,学习模板方法的可能的知识<template><div><div>将下面的msg属性放到上面来:{{msg}}</div><divv-html="htmlMsg"></div><divv-bind="id">这个地方绑定了一个ID{{id}}</div>......
  • van-dialog的stopLoading()方法无效
    问题现象:vant版本1.11.4在van-dialog组件使用时,使用了async-close异步关闭,在点击确认按钮触发confirm的回调时,确认按钮一直处于loading状态<van-dialogshow="{{showDialog}}"title="请输入验证码"use-slotasync-closeshowCancelButtonbind:cancel="dia......
  • 乌班图Ubuntu 24.04初始化MySQL报错error while loading shared libraries: libaio.so
    由于乌班图24.04LTS已经发布了,因此准备新业务逐步往这上面迁移,毕竟支持有效期比22.04更长准备在24.04上进行MySQL的初始化,因为习惯自定义安装存储目录,所以使用mysql-8.0.37-linux-glibc2.28-x86_64.tar.xz这个最新的二进制版本。按照22.04版本整理的安装笔记进行操作,第一步安装......
  • Vue3学习(一)
    创建组件实例:我们传入 createApp 的对象实际上是一个组件import{createApp}from'vue'//从一个单文件组件中导入根组件importAppfrom'./App.vue'constapp=createApp(App)大多数真实的应用都是由一棵嵌套的、可重用的组件树组成的。App(rootcomponent)├......
  • 【前端网页游戏开发】Vue3+PixiJS开发2D闯关打怪游戏,开发时长为6天,成功推出v1.0正式版
    更新内容增加了地图切换功能,扩展了游戏的游玩长度,进入每一关时,击杀10只怪物就会提示通关成功,进入下一关,点击按钮后会恢复玩家的血量,然后重新生成怪物,如果玩家死亡,就会回到提示回到主界面点击后游戏最后增加了BOSS,在玩家进入第四关时会出现BOSS角色,血量为20,玩家伤害为攻击......
  • 七、若依--P17--P18【黑马程序员Java最新AI+若依框架项目开发新方案视频教程,基于RuoYi
    学习视频【黑马程序员Java最新AI+若依框架项目开发新方案视频教程,基于RuoYi-Vue3前后端分离版本,从前端到后端再到AI智能化应用全通关】https://www.bilibili.com/video/BV1pf421B71v/?p=6&share_source=copy_web&vd_source=3949d51b57b2891ea14d6e51c792bef6二次开发P17:新......
  • 04_搭建一个VUE3前端架子+gitee配置
    1.创建一个文件夹HCJV_012.vscode打开该文件夹,打开终端。3.使用vite安装,选择vue,选择JavaScript,项目名称demo01cnpmcreatevite@latest4.跳转demo01目录下cddemo015.安装cnpmcnpminstall尝试执行下:npmrundev6.安装VueRoutercnpminstallvue-router@47.......