這里給大家分享我在網上總結出來的一些知識,希望對大家有所幫助
如何優雅的基于 element-plus,封裝一個夢中情 dialog
優點
擺脫繁瑣的 visible 的命名,以及反復的重復 dom,
想法
將 dialog 封裝成一個函式就能喚起的組件,如下:
addDialog({
title: "測驗", //彈窗名
component: TestVue, //組件
width: "400px", //彈窗大小
props: {
//傳給組件的引數
id: 0
},
callBack: (data: any) => {
//當彈窗任務結束后,呼叫父頁面的回掉函式,(比如我新增完成了需要重繪串列頁面)
console.log("回呼函式", data)
}
})
效果圖
基于 el-dialog 進行初步封裝
// index.ts
import { reactive } from "vue"
type dialogOptions = {
title: string
component: any
props?: Object
width: string
visible?: any
callBack?: Function
}
export const dialogList: dialogOptions[] = reactive([])
export const addDialog = (options: dialogOptions) => {
dialogList.push(Object.assign(options, { visible: true }))
}
export const closeDialog = (item: dialogOptions, i: number, args?: any, isNativeClose?: boolean) => {
dialogList.splice(i, 1)
if (!isNativeClose) item.callBack && item.callBack(...args)
}
<template>
<Teleport to="body">
<el-dialog
v-for="(item, index) in dialogList"
:key="index"
:title="item.title"
:
v-model="item.visible"
@close="() => closeDialog(item, index, '', true)"
>
<component :is="item.component" v-bind="item.props" @close="(...args:any) => closeDialog(item, index, args)" />
</el-dialog>
</Teleport>
</template>
<script setup lang="ts">
import { dialogList, closeDialog } from "./index"
</script>
- 首先定義了 dialogList,它包含了所有彈窗的資訊,
- component 使用 componet is 去動態加載子組件
- addDialog 呼叫喚起彈窗的函式
- closeDialog 關閉彈窗的函式
在app.vue中掛載
<script setup> import Mydialog from "@/components/gDialog/index.vue" </script> <template> <router-view /> <Mydialog></Mydialog> </template> <style scoped> </style>
使用
創建一個彈窗組件
<!-- test.vue -->
<template>
父彈窗
<el-button type="primary" @click="openChildDialog">打開子dialog</el-button>
<el-button type="primary" @click="closeDialog">關閉彈窗</el-button>
</template>
<script setup lang="ts">
import { addDialog } from "@/components/gDialog/index"
import childVue from "./child.vue"
const props = defineProps(["id"])
console.log(props.id, "props")
const emit = defineEmits(["close"])
const closeDialog = () => {
emit("close", 1, 2, 34)
}
const openChildDialog = () => {
addDialog({
title: "我是子dialog",
width: "500px",
component: childVue
})
}
</script>
在串列頁面喚醒彈窗
<!-- list.vue -->
<template>
串列頁
<el-button type="primary" @click="openDialog">打開dialog</el-button>
</template>
<script setup lang="ts">
import { addDialog } from "@/components/gDialog/index"
import TestDialog from "./test.vue"
const openDialog = () => {
addDialog({
title: "我是dialog",
width: "500px",
props:{
id:0
}
component: TestDialog,
callBack: (data: any) => {
//當彈窗任務結束后,呼叫父頁面的回掉函式,(比如我新增完成了需要重繪串列頁面)
console.log("回呼函式", data)
}
})
}
多層級彈窗嵌套
<!-- child.vue -->
<template>
子彈窗
<el-button type="primary" @click="closeDialog">關閉彈窗</el-button>
</template>
<script setup lang="ts">
import { addDialog } from "@/components/gDialog/index"
const emit = defineEmits(["close"])
const closeDialog = () => {
emit("close", 1, 2, 34)
}
</script>
附上代碼
代碼
本文轉載于:
https://juejin.cn/post/7224007334514638903
如果對您有所幫助,歡迎您點個關注,我會定時更新技術檔案,大家一起討論學習,一起進步,

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/552731.html
標籤:其他
上一篇:簡單聊兩句前端模塊化
下一篇:返回列表

