vue3父子傳值實(shí)現(xiàn)彈框功能的示例詳解
更新時(shí)間:2023年12月07日 10:48:28 作者:Kyrie_mvp
這篇文章主要為大家詳細(xì)介紹了vue3如何利用父子傳值實(shí)現(xiàn)彈框功能,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
需在父組件中點(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); //可以測試父傳遞的,子是否拿到了
}
);
</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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue音樂播放器插件vue-aplayer的配置及其使用實(shí)例詳解
本篇文章主要介紹了vue音樂播放器插件vue-aplayer的配置及其使用實(shí)例詳解,具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07
一文詳細(xì)了解Vue?3.0中的onMounted和onUnmounted鉤子函數(shù)
Vue3.0引入了新的組件生命周期鉤子函數(shù)onMounted和onUnmounted,分別用于組件掛載后和卸載前的操作,這些鉤子函數(shù)為開發(fā)者提供了更多靈活性,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-10-10
一文詳解VueUse中useAsyncState的實(shí)現(xiàn)原理
在Vue 3 Composition API中,我們可以使用自定義鉤子函數(shù)來封裝可復(fù)用的邏輯,useAsyncState是一個(gè)用于管理異步狀態(tài)的自定義鉤子函數(shù),本文將給大家介紹一下useAsyncState的實(shí)現(xiàn)原理,感興趣的朋友可以參考下2024-01-01

