vue3 自定義loading的操作方法
使用antdv 后發(fā)現(xiàn)只有button支持loaidng屬性,而其他元素不能使用loading來顯示是否加載中,需要套一層 a-spin 才能支持,非常不方便。
所以寫了個自定義的指令來進行處理
新建loading.vue文件用來頁面顯示
<template>
<div class="loading-container">
<LoadingOutlined />
<p>{{ state.loading.text }}</p>
</div>
</template>
<script lang="ts" setup>
import { LoadingOutlined } from '@ant-design/icons-vue';
import { reactive } from 'vue';
const FONT_SIZE = {
samll: {
icon: '16px',
p: '12px'
},
default: {
icon: '20px',
p: '16px'
},
large: {
icon: '24px',
p: '20px'
}
}
const state = reactive({
loading: {
text: '正在加載中',
fontSize: {
icon: '20px',
p: '16px'
}
} as { text?: string; fontSize?: { icon: string; p: string } }
})
function updateInfo(params: { text: string; size: 'samll' | 'default' | 'large' }) {
state.loading = {
text: params.text,
fontSize: FONT_SIZE[params.size]
}
}
defineExpose({ updateInfo })
</script>
<style lang="scss" scoped>
.loading-container {
position: absolute;
left: 0;
top: 0;
height: 100%;
width: 100%;
overflow: hidden;
background: rgba($color: #ffffff, $alpha: 0.7);
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
font-size: 16px;
color: #335dfd;
z-index: 999999;
:deep(.anticon-loading) {
font-size: 20px;
}
p {
margin-top: 10px;
font-size: 16px;
}
}
</style>在新建個loading.ts 用來注冊v-loading 相關操作
import { createApp, Directive } from 'vue';
import Loading from './index.vue';
/**
* @description 判斷是否為空對象
* **/
export const isEmptyObj = (obj: object): boolean => {
return JSON.stringify(obj) === "{}";
};
/** v-eLoading:[loadingConfig]="state.l||state.a */
const loading: Directive = {
mounted(el, binding) {
const app = createApp(Loading);
const instance = app.mount(document.createElement('div')) as any;
el.instance = instance;
el.style.position = 'relative';
const arg:any = binding.arg
if (!isEmptyObj(arg as any)){
const params = {
text:arg?.text||'正在加載中',
size:'default'
}
instance.updateInfo(params)
}
if (binding.value) {
appendEl(el);
}
},
updated(el, binding) {
console.log(binding.value !== binding.oldValue)
if (binding.value !== binding.oldValue) {
binding.value ? appendEl(el) : removeEl(el);
}
},
};
const appendEl = (el: { appendChild: (arg0: any) => void; instance: { $el: any; }; }) => {
el.appendChild(el.instance.$el);
};
const removeEl = (el: { removeChild: (arg0: any) => void; instance: { $el: any; }; }) => {
el.removeChild(el.instance.$el);
};
export default loading;最后在main.ts 進行注冊
import loadingDirective from 'packages\Loading\index.ts'
createApp(App).directive('loading', loadingDirective).mount('#app')在頁面中就可以直接進行v-loading 進行使用了
<div v-loading="true"></div>
到此這篇關于vue3 自定義loading的文章就介紹到這了,更多相關vue3 自定義loading內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Vue+element+cookie記住密碼功能的簡單實現(xiàn)方法
這篇文章主要給大家介紹了Vue+element+cookie記住密碼功能的簡單實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09
element中TimePicker時間選擇器禁用部分時間(顯示禁用到分鐘)
這篇文章主要介紹了element中TimePicker時間選擇器禁用部分時間(顯示禁用到分鐘),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03
vue的el-select綁定的值無法選中el-option問題及解決
這篇文章主要介紹了vue的el-select綁定的值無法選中el-option問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09

