• 【Vue3】自定义 Vue3 插件(全局实现页面加载动画)


    // main.ts
    import { createApp } from 'vue'
    import App from './App.vue'
    import Loading from "./components/Loading/index.ts";
    
    const app = createApp(App)
    type Lod = {
        show: () => void,
        hide: () => void
    }
    //编写ts loading 声明文件放置报错 和 智能提示
    declare module '@vue/runtime-core' {
        export interface ComponentCustomProperties {
            $loading: Lod
        }
    }
    
    app.use(Loading)
    app.mount('#app')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    <template>
    <!--  App.vue-->
      <div>
        <img id="img" width="400" height="400" src="./assets/unnamed.jpg" alt=""/>
      </div>
    </template>
    
    <script setup lang="ts">
    import {getCurrentInstance} from "vue";
    
    const instance = getCurrentInstance()
    instance?.proxy?.$loading.show()
    setTimeout(() => {
      instance?.proxy?.$loading.hide()
    }, 5000)
    
    </script>
    
    <style>
    
    </style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    // /components/Loading/index.ts
    import type {App, VNode} from 'vue'
    import {createVNode, render} from "vue";
    import Loading from './index.vue'
    
    export default {
        install(app: App) {
            const Vnode: VNode = createVNode(Loading)
            render(Vnode, document.body)
            app.config.globalProperties.$loading = {
                show: Vnode.component?.exposed?.show,
                hide: Vnode.component?.exposed?.hide,
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    <template>
    <!--  components/Loading/index.vue-->
      <div v-if="isShow" class="loading">
        <div class="loading-content">Loading...</div>
      </div>
    </template>
    
    <script setup lang='ts'>
    import { ref } from 'vue';
    const isShow = ref(false) //定位loading 的开关
    
    const show = () => {
      isShow.value = true
    }
    const hide = () => {
      isShow.value = false
    }
    //对外暴露 当前组件的属性和方法
    defineExpose({
      isShow,
      show,
      hide
    })
    </script>
    
    
    
    <style scoped lang="less">
    .loading {
      position: fixed;
      inset: 0;
      background: rgba(0, 0, 0, 0.8);
      display: flex;
      justify-content: center;
      align-items: center;
      &-content {
        font-size: 30px;
        color: #fff;
      }
    }
    </style>
    
    • 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

    在这里插入图片描述

  • 相关阅读:
    微信如何设置自动回复消息,提升沟通效率的?
    SLAM运动模型
    form表单的自定义校验规则
    首家上市的量子计算软件公司!Zapata AI拟完成SPAC交易
    初识Java
    leetcode经典面试150题---5.多数元素
    SPDK/NVMe存储技术分析之SSD设备的发现(二)
    主机ping不通虚拟机,虚拟机可以ping同主机
    解决安装apex报错:No module named ‘packaging‘
    tiup status
  • 原文地址:https://blog.csdn.net/XiugongHao/article/details/136406465