vue3+element-plus?Dialog對(duì)話(huà)框的使用與setup?寫(xiě)法的用法
一. 傳統(tǒng)寫(xiě)法不使用setup語(yǔ)法糖
方式一:通過(guò)v-model的方式實(shí)現(xiàn)子組件的顯示與隱藏
父組件的內(nèi)容
<template>
<div>
<el-button @click="open">打開(kāi)</el-button>
<Child v-model:visible="flag" ></Child>
</div>
</template>
<script>
import { ref, watch } from 'vue'
import Child from "../components/Child.vue"
export default {
components: {
Child
},
setup() {
const flag = ref(false)
const open = () => {
flag.value = true
}
watch(() => flag.value , (val) => {
console.log("監(jiān)聽(tīng)flag值得變化:", val)
})
return {
flag,
open
}
}
}
</script>
子組件內(nèi)容
<template>
<div class="hello">
<el-dialog title="提示" v-model="dialogVisble" width="30%" :before-close="close">
<span>這是一段信息</span>
<template #footer>
<span class="dialog-footer">
<el-button @click="close">取 消</el-button>
<el-button type="primary" @click="confirm">確 定</el-button>
</span>
</template>
</el-dialog>
</div>
</template>
<script>
import { ref, watch } from 'vue'
export default {
props: {
visible: {
type: Boolean,
default: false
}
},
setup(props, ctx) {
const dialogVisble = ref(false)
const close = () => {
ctx.emit("update:visible", false);
};
const confirm = () => {
console.log('你點(diǎn)擊了確定按鈕')
ctx.emit("update:visible", false);
}
watch(() => props.visible, (val) => {
console.log(props.visible, val);
dialogVisble.value = val
});
return {
dialogVisble,
confirm,
close
}
}
}
</script>
方式二:通過(guò)為元素綁定ref的方式實(shí)現(xiàn)子組件的顯示與隱藏
父組件的內(nèi)容
<template>
<div>
<el-button @click="open">打開(kāi)</el-button>
<Child ref="visibleDialog"></Child>
</div>
</template>
<script>
import { ref } from 'vue'
import Child from "../components/Child.vue"
export default {
components: {
Child
},
setup() {
const visibleDialog = ref(null)
const open = () => {
visibleDialog.value.dialogVisble = true
}
return {
visibleDialog,
open
}
}
}
</script>
子組件內(nèi)容
<template>
<div class="hello">
<el-dialog title="提示" v-model="dialogVisble" width="30%" :before-close="close">
<span>這是一段信息</span>
<template #footer>
<span class="dialog-footer">
<el-button @click="close">取 消</el-button>
<el-button type="primary" @click="confirm">確 定</el-button>
</span>
</template>
</el-dialog>
</div>
</template>
<script>
import { ref } from 'vue'
export default {
setup(props, ctx) {
const dialogVisble = ref(false)
const confirm = () => {
console.log('你點(diǎn)擊了確定按鈕')
dialogVisble.value = false
}
const close = () => {
dialogVisble.value = false
}
return {
dialogVisble,
confirm,
close
}
}
}
</script>
2. setup語(yǔ)法糖寫(xiě)法 父組件
<template>
<Child :user="user" ref="visiableDialog"></Child>
<el-button type="primary" @click="openDialog">打開(kāi)彈窗</el-button>
</template>
<script setup>
import { reactive, ref } from 'vue'
import Child from "../components/childComponents.vue"
const visiableDialog = ref(null)
const user = reactive({
name: '張三',
age: 20
})
function openDialog() {
visiableDialog.value.dialogVisble = true
console.log(visiableDialog.value.dialogVisble);
}
</script>子組件
<template>
<div class="hello">{{ `${props.user.name}在學(xué)習(xí)VUE3` }}</div>
<el-dialog title="提示" v-model="dialogVisble" width="30%">
<span>這是一段信息</span>
<template #footer>
<span class="dialog-footer">
<el-button @click="close">取 消</el-button>
<el-button type="primary" @click="confirm">確 定</el-button>
</span>
</template>
</el-dialog>
</template>
<script setup>
import { ref } from 'vue';
import { ElMessageBox } from 'element-plus'
// 定義控制彈窗顯隱的變量
const dialogVisble = ref(false)
// 接受父組件傳過(guò)來(lái)的值
// const props = defineProps({
// user: {
// type: Object,
// default: {}
// }
// })
// 或者
const props = defineProps(['user'])
function confirm() {
ElMessageBox.confirm('確定關(guān)閉嗎?').then(() => {
console.log('你點(diǎn)擊了確定按鈕')
dialogVisble.value = false
}).catch(() => { })
}
function close() {
dialogVisble.value = false
}
// 將變量暴露出來(lái)
defineExpose({
dialogVisble
})
</script>
總結(jié):
- 對(duì)于傳統(tǒng)寫(xiě)法兩種方式來(lái)看,都有各自的優(yōu)缺點(diǎn),方式一在寫(xiě)法上雖然麻煩了些,但是符合vue的設(shè)計(jì)原則,盡量少的操作Dom,以操作數(shù)據(jù)的方式達(dá)到了預(yù)期的目的。
- 而方式二看起來(lái)趨向于我們?cè)趘ue2中的寫(xiě)法,雖然在寫(xiě)法上簡(jiǎn)便,但是在原理上則是操作了Dom,總之,兩種方式都可以達(dá)到我們想要的結(jié)果,至于使用那種方式看個(gè)人編寫(xiě)代碼的習(xí)慣吧。
- 對(duì)于使用setup語(yǔ)法糖寫(xiě)法來(lái)看,代碼整體比較整潔,寫(xiě)起來(lái)也相對(duì)方便快捷
到此這篇關(guān)于vue3+element-plus Dialog對(duì)話(huà)框的使用與setup 寫(xiě)法的用法的文章就介紹到這了,更多相關(guān)vue3 element-plus Dialog對(duì)話(huà)框內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Vue中使用element-ui給按鈕綁定一個(gè)單擊事件實(shí)現(xiàn)點(diǎn)擊按鈕就彈出dialog對(duì)話(huà)框
- vue如何實(shí)現(xiàn)關(guān)閉對(duì)話(huà)框后刷新列表
- vue3動(dòng)態(tài)加載對(duì)話(huà)框的方法實(shí)例
- element-ui和vue表單(對(duì)話(huà)框)驗(yàn)證提示語(yǔ)(殘留)清除操作
- vue+ElementUI 關(guān)閉對(duì)話(huà)框清空驗(yàn)證,清除form表單的操作
- vue+elementui 對(duì)話(huà)框取消 表單驗(yàn)證重置示例
- vue3實(shí)現(xiàn)ai聊天對(duì)話(huà)框功能
相關(guān)文章
功能強(qiáng)大的vue.js拖拽組件安裝應(yīng)用
這篇文章主要為大家介紹了功能強(qiáng)大的vue.js拖拽組件安裝應(yīng)用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
Vue注冊(cè)組件命名時(shí)不能用大寫(xiě)的原因淺析
這段時(shí)間一直在弄vue,當(dāng)然也遇到很多問(wèn)題,這里就來(lái)跟大家分享一些注冊(cè)自定義模板組件的心得 ,需要的朋友可以參考下2019-04-04
vuex 實(shí)現(xiàn)getter值賦值給vue組件里的data示例
今天小編就為大家分享一篇vuex 實(shí)現(xiàn)getter值賦值給vue組件里的data示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
vue項(xiàng)目中?jsconfig.json概念及使用步驟
這篇文章主要介紹了vue項(xiàng)目中?jsconfig.json是什么,本文僅僅簡(jiǎn)單介紹了?jsconfig?.json?的一些基本配置,而?jsconfig?.json提供了大量能使我們快速便捷提高代碼效率的方法,需要的朋友可以參考下2022-07-07
element-ui中實(shí)現(xiàn)tree子節(jié)點(diǎn)部分選中時(shí)父節(jié)點(diǎn)也選中
這篇文章主要介紹了element-ui中實(shí)現(xiàn)tree子節(jié)點(diǎn)部分選中時(shí)父節(jié)點(diǎn)也選中的方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08

