Vue3?函數(shù)式彈窗的實例小結(jié)
運行環(huán)境
- vue3
- vite
- ts
- element-plus
開發(fā)與測試
1. 使用h、render函數(shù)創(chuàng)建Dialog
- 建議可在plugins目錄下創(chuàng)建dialog文件夾,創(chuàng)建index.ts文件,代碼如下
import { h, render } from "vue";
/**
* 函數(shù)式彈窗
* @param component 組件
* @param options 組件參數(shù)
* @returns
*/
function createDialog(component: any, options: any) {
return new Promise((resolve, reject) => {
// 創(chuàng)建一個div節(jié)點
const mountNode = document.createElement("div");
// 將div節(jié)點拼接到Dom的body節(jié)點下
document.body.appendChild(mountNode);
// 使用h函數(shù)創(chuàng)建節(jié)點
const vNode = h(component, {
...options,
// 注意: vue子組件emit回調(diào)事件名稱必須以on開頭
onSubmit: data => {
resolve(data);
// 移除節(jié)點
document.body.removeChild(mountNode);
},
onCancel: data => {
reject(data);
// 移除節(jié)點
document.body.removeChild(mountNode);
}
});
// 渲染Dialog
render(vNode, mountNode);
});
}
export default createDialog;2. 全局掛載函數(shù)式彈窗
在main.ts中引入彈窗,并掛載在app上
// 引入函數(shù)式彈窗 import Dialog from "@/plugins/dialog"; const app = createApp(App); // 掛載到app app.config.globalProperties.$dialog = Dialog;
3. 測試
3.1 創(chuàng)建一個彈窗組件 testDialog.vue
<template>
<el-dialog v-model="dialogVisible" title="測試函數(shù)式彈窗" width="50%">
<span>{{ props.content }}</span>
<template #footer>
<span class="dialog-footer">
<el-button @click="handleCancel">Cancel</el-button>
<el-button type="primary" @click="handleSubmit"> Submit </el-button>
</span>
</template>
</el-dialog>
</template>
<script lang="ts" setup>
import { reactive, toRefs } from "vue";
// 注意: 需要按需引入使用到的第三方UI組件
import { ElDialog, ElButton } from "element-plus";
const props = withDefaults(
defineProps<{
show?: boolean; // moadl開關(guān)
content?: string; // 內(nèi)容
}>(),
{}
);
const emits = defineEmits(["submit", "cancel"]);
const state = reactive({
dialogVisible: props.show
});
const { dialogVisible } = toRefs(state);
/** submit */
const handleSubmit = () => {
// 回調(diào)
emits("submit", { action: "submit", msg: "submit back" });
// 關(guān)閉彈窗
dialogVisible.value = false;
};
/** cancel */
const handleCancel = () => {
// 回調(diào)
emits("cancel", { action: "cancel", msg: "cancel back" });
// 關(guān)閉彈窗
dialogVisible.value = false;
};
</script>3.2 函數(shù)式調(diào)用彈窗
<template>
<!-- 動態(tài)函數(shù)式彈窗 -->
<div class="test_dialog">
<el-button @click="openModal">調(diào)用函數(shù)式彈窗</el-button>
</div>
</template>
<script lang="ts" setup>
import { getCurrentInstance } from "vue";
import TestDialog from "./testDialog.vue";
// 通過全局的上下文拿到 proxy 屬性
const { proxy } = getCurrentInstance();
// 調(diào)用函數(shù)式彈窗
const openModal = () => {
// 調(diào)用彈窗
proxy
.$dialog(TestDialog, {
show: true,
content: "調(diào)用彈窗成功了!"
})
.then(res => {
// submit
console.log(res);
})
.catch(error => {
// cancel 回調(diào)
console.log(error);
});
};
</script>
<style lang="scss" scoped>
.test_dialog {
padding: 50px;
}
</style>3.3 測試效果

問題
非原生的html元素?zé)o法渲染,如elements-plus組件無法在彈窗渲染
因為使用h函數(shù)無法渲染第三方UI,需要在彈窗中單獨引入,如上面測試代碼使用的element-plus的modal和button都需要按需引入一次。如果沒有引入彈窗都不會show出來,控制臺會給于警告如下截圖,通過這個截圖也可以看到,h函數(shù)是幫我們將彈窗組件拼接到了DOM中,組件的參數(shù)一并拼接了進去,與傳統(tǒng)的調(diào)用方式近似。

在調(diào)用dialog的代碼中,ts會有代碼警告
可以全局申明下掛載的dialog,可直接在main.ts添加下面的申明
// 全局申明下$dialog,可以去除調(diào)用時ts的警告
declare module "vue" {
export interface ComponentCustomProperties {
$dialog: any;
}
}到此這篇關(guān)于Vue3 函數(shù)式彈窗的文章就介紹到這了,更多相關(guān)Vue3 函數(shù)式彈窗內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3.0?移動端二次封裝van-uploader實現(xiàn)上傳圖片(vant組件庫)
這篇文章主要介紹了vue3.0?移動端二次封裝van-uploader上傳圖片組件,此功能最多上傳6張圖片,并可以實現(xiàn)本地預(yù)覽,實現(xiàn)代碼簡單易懂,需要的朋友可以參考下2022-05-05
使用van-picker?動態(tài)設(shè)置當前選中項
這篇文章主要介紹了使用van-picker?動態(tài)設(shè)置當前選中項方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10

