1.在父组件,点击一个按钮,弹窗出现,把弹窗当做子组件
<template>
<div>
<a-button type="primary" @click="add">新增</a-button>
<add-material v-model="fieldData.addVisible"></add-material>
</div>
</template>
<script setup>
import addMaterial from './components/add-material.vue';
import { ref } from 'vue';
const fieldData = ref({
addVisible: false,
});
const add = () => {
fieldData.value.addVisible= true;
};
</script>
<style lang="less" scoped></style>
2.子组件
<template>
<div>
<a-modal v-model:visible="modalVisible" title="Basic Modal" @ok="handleOk" @cancel="cancel">
<p>Some contents...</p>
<p>Some contents...</p>
<p>Some contents...</p>
</a-modal>
</div>
</template>
<script setup>
import { defineProps, defineEmits } from 'vue';
const props = defineProps({
modelValue: {
type: Boolean,
},
});
const emit = defineEmits(['update:modelValue']);
const modalVisible = computed({
get: () => props.modelValue,
set: v => emit('update:modelValue', v),
});
// 确定按钮的回调
const handleOk = () => {
console.log('%c Line:21 🧀', 'color:#6ec1c2');
};
</script>