Vue3全局掛載Dialog組件的示例代碼
一、背景描述
最近項(xiàng)目中遇到了全局掛載Dialog的需求,在這里記錄一下,希望可以幫到大家。
二、Dialog組件封裝

2.1 Dialog.vue
<template>
<!-- 刪除文件對(duì)話框 -->
<el-dialog
:title="dialogTitle"
v-model="dialogVisible"
:width="dialogWidth"
:before-close="handleBeforeClose"
>
<div>{{ title }}</div>
<template #footer>
<div class="dialog-footer">
<el-button @click="handleDialogClose">取 消</el-button>
<el-button
type="primary"
:loading="sureBtnLoading"
@click="handleDialogSure"
>確 定</el-button
>
</div>
</template>
</el-dialog>
</template>
<script setup>
import {
ref,
computed
} from 'vue'
import {
deleteFile
} from '@/api/file'
import { ElMessage } from 'element-plus';
const props = defineProps({
fileInfo: {
type: Array,
default: [],
},
title: {
type: String,
default: "",
},
width: {
type: String,
default: "550px",
},
saveClick: {
type: Function,
default: () => {},
},
cancelClick: {
type: Function,
default: () => {},
},
remove: {
type: Function,
default: () => {},
}
})
const dialogVisible = ref(false) // 窗體顯示控制
const dialogWidth = computed(() => {
return props.width || "550px"
})
const dialogTitle = computed(() => {
return props.title || '刪除文件'
})
const title = computed(() => {
return '此操作將永久刪除, 是否繼續(xù)?'
})
const sureBtnLoading = ref(false)
// 關(guān)閉回調(diào)觸發(fā)事件
const handleBeforeClose = (done) => {
done()
}
// 取消按鈕點(diǎn)擊事件
const handleDialogClose = () => {
props.cancelClick()
props.remove()
}
// 確定按鈕點(diǎn)擊事件
const handleDialogSure = () => {
sureBtnLoading.value = true
deleteFile({
id: props.fileInfo.id
})
.then((res) => {
sureBtnLoading.value = false
ElMessage.success(res.data || '刪除成功')
props.saveClick()
props.remove()
})
.catch(() => {
sureBtnLoading.value = false
})
}
</script>
2.2 index.js
import DeleteFileDialog from './Dialog.vue'
import { createApp } from 'vue'
let mountNode
let createMount = (opts) => {
if(mountNode){ // 確保只存在一個(gè)彈框
document.body.removeChild(mountNode)
mountNode = null
}
mountNode = document.createElement('div')
document.body.appendChild(mountNode)
const app = createApp(DeleteFileDialog, {
...opts,
modelValue: true,
remove() { // 傳入remove函數(shù),組件內(nèi)可移除dom 組件內(nèi)通過(guò)props接收
app.unmount(mountNode)
document.body.removeChild(mountNode)
mountNode = null
}
})
return app.mount(mountNode)
}
function DeleteFile(options = {}) {
options.id = options.id || 'deleteFile_' + 1 //唯一id 刪除組件時(shí)用于定位
let $init = createMount(options)
return $init
}
DeleteFile.install = app => {
app.component('delete-file-dialog', DeleteFileDialog)
app.config.globalProperties.$deleteFileDialog = DeleteFile
}
export default DeleteFile
三、全局掛載Dialog組件
在main.js中進(jìn)行全局掛載
import DeleteFileDialog from '@/components/dialog/deleteFile/index.js' app.use(DeleteFileDialog)
四、使用Dialog組件
proxy.$deleteFileDialog({
fileInfo: fileInfo,
saveClick: () => {
console.log('saveClick---')
},
cancelClick: () => {
console.log('cancelClick---')
}
})
五、效果圖

到此這篇關(guān)于Vue3全局掛載Dialog組件的示例代碼的文章就介紹到這了,更多相關(guān)Vue3全局掛載Dialog內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決vue請(qǐng)求接口第一次成功,第二次失敗問(wèn)題
這篇文章主要介紹了解決vue請(qǐng)求接口第一次成功,第二次失敗問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
使用Vue3和ApexCharts實(shí)現(xiàn)3D徑向條形圖的代碼
徑向條形圖是一種用于可視化單一數(shù)據(jù)點(diǎn)及其與目標(biāo)或理想值的關(guān)系的圖表類(lèi)型,它在顯示進(jìn)度、完成率或其他類(lèi)似度量時(shí)非常有用,本文給大家介紹了使用Vue3和ApexCharts實(shí)現(xiàn)3D徑向條形圖,感興趣的小伙伴可以參考閱讀下2024-06-06
Vue項(xiàng)目分包打包配置(包含dev)完整過(guò)程
最近接到一個(gè)需求,公司需要對(duì)vue項(xiàng)目實(shí)現(xiàn)線上打包,下面這篇文章主要給大家介紹了關(guān)于Vue項(xiàng)目分包打包配置(包含dev)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06
vue3成功創(chuàng)建項(xiàng)目后?run?serve啟動(dòng)項(xiàng)目報(bào)錯(cuò)的解決
這篇文章主要介紹了vue3成功創(chuàng)建項(xiàng)目后?run?serve啟動(dòng)項(xiàng)目報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
Vue使用輪詢定時(shí)發(fā)送請(qǐng)求代碼
這篇文章主要介紹了Vue使用輪詢定時(shí)發(fā)送請(qǐng)求代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
使用WebStorm用Debug模式調(diào)試Vue等前端項(xiàng)目的步驟
WebStorm提供了更簡(jiǎn)單的前端調(diào)試方法,記錄一下WebStorm調(diào)試步驟啟動(dòng)前端項(xiàng)目,這篇文章主要介紹了使用WebStorm用Debug模式調(diào)試Vue等前端項(xiàng)目的步驟,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2024-11-11
vue?內(nèi)置組件?component?的用法示例詳解
這篇文章主要介紹了vue內(nèi)置組件component的用法,本文給大家介紹了component內(nèi)置組件切換方法,通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08
vue中element-ui表格縮略圖懸浮放大功能的實(shí)例代碼
element-ui界面非常簡(jiǎn)潔和美觀,提供的組件可以滿足絕大多數(shù)的應(yīng)用場(chǎng)景,當(dāng)表格中顯示了圖片的縮略圖時(shí),想要鼠標(biāo)浮動(dòng)在縮略圖上時(shí)放大圖片的效果,該如何實(shí)現(xiàn)呢?下面小編通過(guò)實(shí)例代碼給大家介紹vue中element-ui表格縮略圖懸浮放大功能,一起看看吧2018-06-06

