vue3父子传值实现弹框功能的示例详解
更新时间:2023年12月07日 10:48:28 作者:Kyrie_mvp
这篇文章主要为大家详细介绍了vue3如何利用父子传值实现弹框功能,文中的示例代码讲解详细,具有一定的借鉴价值,感兴趣的小伙伴可以跟随小编一起学习一下
需在父组件中点击按钮然后弹出子组件的弹框
1 父组件
<template> <generateDialog :drawer="drawer"></generateDialog> //bind绑定子组件 {{ drawer }} // 看是否父更改了状态 <el-button @click="CanShowDrawer">点击显示弹窗</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渲染是重点
<template> <el-dialog title="我是标题" :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>
效果图如下
子的完整代码如下 (父已经是完整代码了)
<template> <div class="dialog"> <el-dialog title="我是标题" :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>
到此这篇关于vue3父子传值实现弹框功能的示例详解的文章就介绍到这了,更多相关vue3弹框内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Electron+vue3项目使用SQLite3数据库详细步骤(超详细)
Electron是一个基于vue.js的新框架,它可以构建桌面应用,这篇文章主要给大家介绍了关于Electron+vue3项目使用SQLite3数据库的详细步骤,文中通过代码介绍的非常详细,需要的朋友可以参考下2024-01-01Vue中的Object.defineProperty全面理解
这篇文章主要介绍了Vue中的Object.defineProperty全面理解,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2022-04-04vuex之this.$store.dispatch()与this.$store.commit()的区别及说明
这篇文章主要介绍了vuex之this.$store.dispatch()与this.$store.commit()的区别及说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2023-06-06
最新评论