Vue + TS笔记:sync修饰符和 @Emit(‘upate:visible‘)

父组件

<Father>
	<Child visible.sync="showChild" ></Child>
</Father>

子组件

// Child 组件
<template>
	<div class="child-component" v-show="visible">
		<i class="icon-close" @click="closeHandle"></i>
	</div>
</template>

@Emit用法

// Child 组件
import { Emit, Prop } from "vue-property-decorator";
// Child 组件
// 弹窗是否显示
@Prop({ default: false })
visible!: boolean;


@Emit('update:visible') 
updateVisible(bool:boolean) {
    return bool;
}

closeHandle() {
	this.updateVisible(false)
}

在父组件引入子组件的时候,要在子组件的属性(如:visible)后加上.sync,才能让子组件内部的@Emit(‘update:visible’) 生效。update:xxx,xxx可以是子组件prop中的任意属性,并在父组件中引入的子组件的属性xxx后,加上.sync。如 <Child xxx.sync="属性值"></Child>

相当于js:

// Child 组件
// 弹窗是否显示
props: {
	visible: {
		type:Boolean,
		default: false
	}
}

methods: {
	updateVisible(bool) {
		this.$emit('update:visible',bool)
	}
	closeHandle() {
		this.updateVisible(false)
	}
}
一些资料:

彻底明白VUE修饰符sync:https://www.jianshu.com/p/d42c508ea9de

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值