Vue中.sync语法糖

在组件传值中 本篇文章只关注子组件给父组件传值 也就是自定义事件的方式

其实简单来说,.sync修饰符就是用来在子组件中修改父组件传进来的值

下面是我对这个修饰符的理解:

父组件:

<template>
    <div>
        <input type="button"
               value="我是父组件中的按钮"
               @click="show">
        <child @upIsShow="changeIsShow" v-show="isShow"/>
    </div>
</template>
 
<script>
    import child from "@/components/child"
    export default {
        data() {
            return {
                isShow:false
            }
        },
        components:{
            child
        },
        methods:{
            show(){
                this.isShow=true;
            },
            changeIsShow(bol){
                this.isShow=bol;
            }
        }
    }
</script>

子组件:

<template>
    <div>
         我是子组件
        <input type="button" @click="upIsShow">
    </div>
</template>
<script>
    export default {
        methods:{
            upIsShow(){
                this.$emit("upIsShow",false);
            }
        }
    }
</script>

上述方法是传统的,父组件绑定自定义事件,由子组件去触发自定义事件,接收到子组件传过来的值,然后对父组件的数据加以修改

下面对.sync的解析需要一点Vue3的知识

首先将父组件绑定的自定义事件修改为:
 

<child @update:isShow="changeIsShow" v-show="isShow"/>

子组件也做对应调整:

upIsShow(){
    this.$emit("update:isShow",false);
}

然后将父组件的changeIsShow直接写成匿名函数:

<child @update:isShow="function(bol){isShow=bol}" v-show="isShow"/>

再将函数体改为箭头函数:
 

<child @update:isShow="bol=>isShow=bol" v-show="isShow"/>

最后变成这个样子:

<child :isShow.sync="isShow" v-show="isShow"/>

:isShow.sync="isShow"其实是 @update:isShow="bol=>isShow=bol"语法糖。是其一种简写形式

如果觉得以上代码太麻烦或者直接就是看不懂的状态

那么应该去学习一下vue3关于v-model的一些改动,可能会对你有一些帮助;

完整代码:

<template>
    <div>
        <input type="button"
               value="我是父组件中的按钮"
               @click="show">
        <child :isShow.sync="isShow" v-show="isShow"/>
    </div>
</template>
 
<script>
    import child from "@/components/child"
    export default {
        data() {
            return {
                isShow:false
            }
        },
        components:{
            child
        },
        methods:{
            show(){
                this.isShow=true;
            },
            changeIsShow(bol){
                this.isShow=bol;
            }
        }
    }
</script>

父组件👆

子组件 👇

<template>
    <div>
         我是一个子组件,我在红色的海洋里!
        <input type="button" value="点我隐身" @click="upIsShow">
    </div>
</template>
 
<script>
    export default {
        methods:{
            upIsShow(){
                this.$emit("update:isShow",false);
            }
        }
    }
</script>

总结

sync简单来说就是为子组件修改父组件中的数据提供了简便的方式,以后使用子组件修改父组件的某些数据的时候,就可以使用sync了

注意

vue3似乎已经废弃了sync修饰符,具体的信息可以去官方文档查看

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue ,.sync 是一个语法糖,它能够简化父子组件之间的数据传递。.sync 修饰符实际上是一个双向绑定的简写形式,它会自动创建一个名为 update:propName 的自定义事件,并且在父组件监听这个事件,然后更新子组件的数据。 例如,在父组件使用子组件时,可以通过 v-bind.sync 修饰符将父组件的数据与子组件的数据绑定在一起: ``` <template> <ChildComponent :message.sync="messageFromParent"></ChildComponent> </template> ``` 这里的 .sync 修饰符会自动将子组件的 message 属性与父组件的 messageFromParent 属性进行双向绑定,并且在父组件监听名为 update:message 的自定义事件,以更新父组件的数据。 ``` <template> <ChildComponent :message.sync="messageFromParent"></ChildComponent> <button @click="updateMessage">Update Message</button> </template> <script> export default { data() { return { messageFromParent: 'Hello World' } }, methods: { updateMessage() { this.messageFromParent = 'Hello Vue' } } } </script> ``` 在子组件,可以通过 $emit() 方法触发 update:message 事件,以更新父组件的数据。 ``` <template> <div> <input type="text" v-model="message"> <button @click="$emit('update:message', message)">Update Message</button> </div> </template> <script> export default { props: { message: { type: String } } } </script> ``` 这样,父组件和子组件之间就可以通过 .sync 修饰符实现双向数据绑定了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值