父子组件实现弹窗以及组件间的通信

方法一:传统的typescript

<!-- ParentComponent.vue -->
<template>
  <div>
    <h2>Parent Component</h2>
    <button @click="showDialog = true">Show Dialog</button>
    <ChildComponent :visible="showDialog" @close="showDialog = false" />
  </div>
</template>

<script lang="ts">
  import { defineComponent, ref } from 'vue';
  import ChildComponent from './ChildComponent.vue';

  export default defineComponent({
    name: 'ParentComponent',
    components: { ChildComponent },
    setup() {
      const showDialog = ref(false);
      return {
        showDialog,
      };
    },
  });
</script>
<!-- ChildComponent.vue -->
<template>
  <n-dialog v-model:value="visible" @close="onClose">
    <div>Child Component</div>
  </n-dialog>
</template>

<script lang="ts">
  import { defineComponent, PropType } from 'vue';
  import { NDialog } from 'naive-ui';

  export default defineComponent({
    name: 'ChildComponent',
    components: { NDialog },
    props: {
      visible: {
        type: Boolean as PropType<boolean>,
        required: true,
      },
    },
    emits: ['close'],
    setup(props, { emit }) {
      const onClose = () => {
        emit('close');
      };

      return {
        onClose,
      };
    },
  });
</script>

方法二:使用了setup的typescript

<!-- ChildComponent.vue -->
<script lang="ts">
// 导出组件
export default {
	name: 'Profile'
}
</script>
<script lang="ts" setup>
// 导入组件
import { NModal } from 'naive-ui'
import {reactive} from 'vue'
// 定义接受的参数,父组件想下传递visible
const props = defineProps(['visible'])
// 定义回调方法(要调用父组件的close方法)
const emit = defineEmits(['close'])
// 定义子组件中的数据模型
const model = reactive({
	visible: false,
})
// 将从父组件接受的参数传递给子组件模型赋值
const handleInit = () => {
	model.visible = props.visible
}
// 定义子组件的方法,该方法调用父组件的close方法,修改父组件的visible参数
const handleClose = () => {
	emit('close')
}
// 调用初始化参数方法
handleInit()

</script>

<template>
  <NModal
    v-model:show="model.visible"     // 使用visible控制显示和隐藏
    :mask-closable="false"
		title='Profile'
    preset="dialog"
		@close="handleClose"             // 关闭时调用handleClose方法
  >
      你好你好
  </NModal>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import Profile from '@/components/common/Profile/index.vue'
  const showProfile = ref(false)
</script>
<template>
  <div>
    <h2>Parent Component</h2>
    <button @click="showProfile = true">Show Dialog</button>
    <Profile v-if="showProfile" v-model:visible="showProfile" @close="showProfile = false" />
  </div>
</template>

简单来说就是父组件通过插值props向下传递参数(showProfile实参,visible形参),控制子组件的显示和隐藏。(但是这个visible不能直接使用,将其传递给了子组件中的参数visible)
子组件通过调用emit父组件的方法(close方法),修改父组件的参数(show)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tinpo_123

感谢给小张填杯java~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值