antd vue modal使用注意点

使用antd vue,当需要把modal封装成独立组件时,一般组件的显示与否是通过父组件传props过来,关闭再用this.$emit()传参给父组件做出相应变化。
父组件


<template>
	<a-button @click="showModal">show modal</a-button>
	<son-modal :visible="showModal" @cancel="handleCancel"></son-modal>
</template>

<script>
	import SonModal from './SonModal'
	export default{
		data(){
			return {
				showModal:false
			}
		},
		methods:{
			showModal(){
				this.showModal=true
			},
			handleCancel(){
				this.showModal=false
			}
		}
	}
</script>

子组件

<template>
	<a-modal :visible=visible @cancel="handleCancel"></a-modal>
</template>

<script>
	export default{
		props:{
			visible:{
				default:false
			}
		},
		methods:{
			handleCancel(){
				this.$emit('cancel')
			}
		}
	}
</script>

这种方式的话,点击modal右上角的X关闭时,会报错:
[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop’s value. Prop being mutated: “visible”
**

解决办法:利用vue $refs实现

**
父组件


<template>
	<a-button @click="showModal">show modal</a-button>
	<son-modal ref="modal"></son-modal>
</template>

<script>
	import SonModal from './SonModal'
	export default{
		data(){
			return {}
		},
		methods:{
			showModal(){
				this.$refs.modal.show()
			}
		}
	}
</script>

子组件

<template>
	<a-modal :visible=visible @cancel="handleCancel"></a-modal>
</template>

<script>
	export default{
		data(){
			return{
				visible:false
			}
		},
		methods:{
			show(){
				this.visible=true
			},
			handleCancel(){
				this.visible=false
			}
		}
	}
</script>

antdvue3中,要使resetFields生效,需要在modal关闭时调用resetFields方法。具体的代码实现如下: ``` <template> <div> <a-button @click="openModal">打开modal</a-button> <a-modal v-model:visible="visible" @ok="handleOk" @cancel="handleCancel"> <a-form ref="form" :model="form"> <a-form-item label="姓名" :rules="{ required: true, message: '请输入姓名' }"> <a-input v-model:value="form.name" /> </a-form-item> <a-form-item label="年龄" :rules="{ required: true, message: '请输入年龄' }"> <a-input-number v-model:value="form.age" /> </a-form-item> </a-form> </a-modal> </div> </template> <script> import { ref } from 'vue'; import { Button, Modal, Form, Input, InputNumber } from 'ant-design-vue'; export default { components: { 'a-button': Button, 'a-modal': Modal, 'a-form': Form, 'a-form-item': Form.Item, 'a-input': Input, 'a-input-number': InputNumber, }, setup() { const visible = ref(false); const form = ref({ name: '', age: null }); const openModal = () => { visible.value = true; }; const handleOk = () => { // 提交表单 form.value = { name: '', age: null }; visible.value = false; }; const handleCancel = () => { // 关闭modal时调用resetFields方法 const formRef = this.$refs.form; formRef.resetFields(); form.value = { name: '', age: null }; visible.value = false; }; return { visible, form, openModal, handleOk, handleCancel, }; }, }; </script> ``` 在handleCancel方法中,我们首先获取form的ref,然后调用resetFields方法重置表单数据,最后将form的值设置为空对象{},将visible的值设置为false,关闭modal。这样就能够使resetFields生效了。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值