Vue3自定義插件實(shí)現(xiàn)全局Loading效果過程
使用組件的形式
實(shí)現(xiàn)全局loading效果:

在項(xiàng)目中添加一個(gè)文件夾
plugin/loading,并新建一個(gè)loading.vue組件:

loading.vue組件代碼:
<template>
<div v-if="loading" class="loading">Loading</div>
</template>
<script setup lang="ts">
import { ref } from 'vue'
const loading = ref(false)
const show = () => {
loading.value = true
console.log('show方法執(zhí)行了')
}
const hide = () => {
loading.value = false
console.log('hide方法執(zhí)行了')
}
defineExpose({
show,
hide
})
</script>
<style scoped>
.loading {
position: fixed;
top: 0;
display: flex;
align-items: center;
justify-content: center;
font-size: 30px;
width: 100vw;
height: 100vh;
opacity: 0.5;
background-color: grey;
color: white;
z-index: 999;
}
</style>
新建一個(gè)index.ts插件文件
將loading組件加載進(jìn)去,并且給vue對(duì)象全局掛載LOADING屬性
import type { App } from 'vue'
import { createVNode, render } from 'vue'
import loading from './loadIng.vue'
export default {
install(app: App) {
console.log('全局Loadig插件執(zhí)行了')
// 將vue組件轉(zhuǎn)為VNode,然后渲染到頁面上
const VNode = createVNode(loading)
render(VNode, document.body)
// 給Vue對(duì)象全局掛載屬性show、hide
app.config.globalProperties.LOADING = {
show: VNode.component?.exposed?.show,
hide: VNode.component?.exposed?.hide
}
console.log('掛載點(diǎn)屬性:', VNode.component?.exposed, app.config.globalProperties.LOADING)
}
}
在main.ts中掛載這個(gè)組件

在main.ts中聲明LOADING屬性
import './assets/main.css'
import { createApp } from 'vue'
import { createPinia } from 'pinia'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import App from './App.vue'
import router from './router'
import loading from './plugin/loading'
const app = createApp(App)
app.use(createPinia())
app.use(router)
// 自定義插件全局loading
app.use(loading)
app.use(ElementPlus)
// 定義全局變量和全局函數(shù)
app.config.globalProperties.env = "dev"
app.config.globalProperties.filter = {
format<T>(content:T){
return "格式化后的內(nèi)容" + content
}
}
// 解決Vue中的全局變量和函數(shù)報(bào)錯(cuò)問題
interface Filter {
format<T>(content:T):string
}
interface LOADING {
show():null
hide():null
}
declare module 'vue' {
export interface ComponentCustomProperties {
filter: Filter,
env:string,
LOADING:LOADING
}
}
app.mount('#app')
在App.vue中引用這個(gè)插件
<template>
<div class="app">
主頁測試全局loading插件
<button @click="showLoading">點(diǎn)擊加載loading</button>
</div>
</template>
<script setup lang="ts">
import { getCurrentInstance } from 'vue'
const app = getCurrentInstance()
const showLoading = ()=>{
// const app = getCurrentInstance()
app?.proxy?.LOADING.show()
console.log("點(diǎn)擊了loading", app);
// 5秒之后取消加載
setTimeout(() => {
app?.proxy?.LOADING.hide()
}, 5000);
}
</script>
<style scoped>
.app{
width: 100vw;
height: 100vh;
font-size: 30px;
background-color: orange;
}
</style>
最后的效果就是:

總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue中使用vue-plugin-hiprint插件進(jìn)行打印的功能實(shí)現(xiàn)
hiprint 是一個(gè)web 打印的js組件,無需安裝軟件,支持windows,macOS,linux 系統(tǒng),支持移動(dòng)端,PC端瀏覽器,angular,vue,react 等 分頁預(yù)覽,打印,操作簡單,運(yùn)行快速,本文介紹了Vue中使用vue-plugin-hiprint插件進(jìn)行打印,需要的朋友可以參考下2025-04-04
基于vue項(xiàng)目設(shè)置resolves.alias: ''@''路徑并適配webstorm
這篇文章主要介紹了基于vue項(xiàng)目設(shè)置resolves.alias: '@'路徑并適配webstorm,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12
Vue.js組件使用props傳遞數(shù)據(jù)的方法
這篇文章主要為大家詳細(xì)介紹了Vue.js組件使用props傳遞數(shù)據(jù)的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10
vue定時(shí)器設(shè)置和關(guān)閉頁面時(shí)關(guān)閉定時(shí)器方式
這篇文章主要介紹了vue定時(shí)器設(shè)置和關(guān)閉頁面時(shí)關(guān)閉定時(shí)器方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
vue?LogicFlow更多配置選項(xiàng)示例詳解
這篇文章主要為大家介紹了vue?LogicFlow更多配置選項(xiàng)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
vue實(shí)現(xiàn)數(shù)字滾動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)數(shù)字滾動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06

