vue3父子傳值實(shí)現(xiàn)彈框功能的示例詳解
需在父組件中點(diǎn)擊按鈕然后彈出子組件的彈框
1 父組件
<template> <generateDialog :drawer="drawer"></generateDialog> //bind綁定子組件 {{ drawer }} // 看是否父更改了狀態(tài) <el-button @click="CanShowDrawer">點(diǎn)擊顯示彈窗</el-button> </template> <script setup> import generateDialog from './components/useModel/generateDialog.vue'; //引入子 const drawer = ref(false); //顯示彈窗 const CanShowDrawer = () => { drawer.value = !drawer.value; };
2 子組件中的v-model渲染是重點(diǎn)
<template> <el-dialog title="我是標(biāo)題" :with-header="false" direction="ttb" size="300px" v-model="drawerson"> </el-dialog> </template> <script setup> import { defineProps, watch, ref } from 'vue'; const drawerson = ref(false); const props = defineProps({ drawer: { type: Boolean, default: false, }, }); watch( () => props.drawer, (newVal) => { drawerson.value = newVal; console.log('drawerson.value', drawerson.value); //可以測(cè)試父?jìng)鬟f的,子是否拿到了 } ); </script>
效果圖如下
子的完整代碼如下 (父已經(jīng)是完整代碼了)
<template> <div class="dialog"> <el-dialog title="我是標(biāo)題" :with-header="false" direction="ttb" size="300px" v-model="drawerson"> <div class="top">我是頂部</div> <div class="father"> <el-scrollbar class="scrollbar"> <div class="son">1</div> <div class="son">2</div> <div class="son">3</div> <div class="son">4</div> <div class="son">5</div> <div class="son">6</div> </el-scrollbar> </div> <div class="footer"> <el-button>生成</el-button> </div> </el-dialog> </div> </template> <script setup> import { defineProps, watch, ref } from 'vue'; const drawerson = ref(false); const props = defineProps({ drawer: { type: Boolean, default: false, }, }); watch( () => props.drawer, (newVal) => { drawerson.value = newVal; console.log('drawerson.value', drawerson.value); } ); </script> <style lang="less" scoped> .dialog { position: relative; .top { text-align: center; padding-bottom: 20px; } .father { .scrollbar { height: 300px; } .son { min-width: 213px; height: 200px; background: #aaa; margin: 0 10px 10px 10px; display: flex; justify-content: center; align-items: center; } } .footer { position: absolute; bottom: 10px; right: 3%; } } :deep(.el-dialog) { width: 800px; margin: 0 auto; } :deep(.el-scrollbar__view) { display: flex; flex-wrap: wrap; height: 80%; } </style>
到此這篇關(guān)于vue3父子傳值實(shí)現(xiàn)彈框功能的示例詳解的文章就介紹到這了,更多相關(guān)vue3彈框內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue實(shí)現(xiàn)在線預(yù)覽pdf文件功能(利用pdf.js/iframe/embed)
項(xiàng)目要求需要預(yù)覽pdf文件,網(wǎng)上找了很久,發(fā)現(xiàn)pdf.js的效果,這篇文章主要給大家介紹了關(guān)于Vue實(shí)現(xiàn)在線預(yù)覽pdf文件功能,主要利用pdf.js/iframe/embed來(lái)實(shí)現(xiàn)的,需要的朋友可以參考下2021-06-06Vue CLI3搭建的項(xiàng)目中路徑相關(guān)問(wèn)題的解決
這篇文章主要介紹了Vue CLI3搭建的項(xiàng)目中路徑相關(guān)問(wèn)題的解決,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09vue中使用vue-print.js實(shí)現(xiàn)多頁(yè)打印
這篇文章主要介紹了vue中使用vue-print.js實(shí)現(xiàn)多頁(yè)打印,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03vue使用拖拽方式創(chuàng)建結(jié)構(gòu)樹(shù)
這篇文章主要為大家詳細(xì)介紹了vue使用拖拽方式創(chuàng)建結(jié)構(gòu)樹(shù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10Vue實(shí)現(xiàn)監(jiān)聽(tīng)某個(gè)元素滾動(dòng),親測(cè)有效
這篇文章主要介紹了Vue實(shí)現(xiàn)監(jiān)聽(tīng)某個(gè)元素滾動(dòng),親測(cè)有效!具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07vue3使用pdf.js來(lái)預(yù)覽文件的操作步驟(本地文件測(cè)試)
這篇文章主要介紹了vue3使用pdf.js來(lái)預(yù)覽文件的操作步驟(本地文件測(cè)試),文中通過(guò)代碼示例和圖文結(jié)合的方式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-05-05Vue實(shí)現(xiàn)簡(jiǎn)單圖片切換效果
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)簡(jiǎn)單圖片切換效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06VUEJS實(shí)戰(zhàn)之構(gòu)建基礎(chǔ)并渲染出列表(1)
這篇文章主要為大家詳細(xì)介紹了VUEJS實(shí)戰(zhàn)之構(gòu)建基礎(chǔ)并渲染出列表,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06