Preface

The first time I met .sync modifier, I didn't know it very well. So, I seldom use that. Today, I am going to get it.

Main

In the past, I use “two-way binding” like this:

```<div class="app" id="app">
<button-counter :child-count="parCount" @add="add"></button-counter>
<p>parent component {{parCount}}</p>
</div>
```


let app = new Vue({
el: '#app',
data: {
parCount: 0
},
methods: {
add() {
this.parCount++
}
},
components: {
'button-counter': {
template:
'&lt;button @click="add"&gt;You clicked me {{ childCount }} times.&lt;/button&gt;',
props: ['childCount'],
methods: {
add() {
this.$emit('add')
}
}
}
}
})

It was easy to understand except a little inconvenient. I need to listen and handle event in child and parent component. Also

true two-way binding can create maintenance issues, because child components can mutate the parent without the source of that mutation being obvious in both the parent and the child.

So, Vue recommends emitting events in the pattern of update:myPropName. For example:

```<div class="app" id="app">
<button-counter :child-count="parCount" @update:child-count="parCount=$event"></button-counter>
<p>parent component {{parCount}}</p>
</div>
```


let app = new Vue({
el: '#app',
data: {
parCount: 0
},
methods: {},
components: {
'button-counter': {
template:
'&lt;button @click="add"&gt;You clicked me {{ childCount }} times.&lt;/button&gt;',
props: ['childCount'],
methods: {
add() {
this.$emit('update:child-count', this.childCount + 1) // child-count is right while childCount won't work
}
}
}
}
})

See? In this case, we don't have to add event callback in parent component because vue have done that. And this is the principle of .sync. For convenience, Vue offers a shorthand for this pattern with the .sync modifier which would make the code above like:

```<div class="app" id="app">
<button-counter :child-count.sync="parCount"></button-counter>
<p>parent component {{parCount}}</p>
</div>
```


let app = new Vue({
el: '#app',
data: {
parCount: 0
},
methods: {},
components: {
'button-counter': {
template:
'&lt;button @click="add"&gt;You clicked me {{ childCount }} times.&lt;/button&gt;',
props: ['childCount'],
methods: {
add() {
this.$emit('update:childCount', this.childCount + 1) // childCount is right while child-count won't work
}
}
}
}
})

Also,

The .sync modifier can also be used with v-bind when using an object to set multiple props at once:

```<text-document v-bind.sync="doc"></text-document>
```

This passes each property in the doc object (e.g. title) as an individual prop, then adds v-on update listeners for each one.

For more information, go Vue.js

Original Post

原文地址:https://segmentfault.com/a/1190000017107941

Understand .sync in Vue的更多相关文章

  1. 深入理解vue 修饰符sync【 vue sync修饰符示例】

    在说vue 修饰符sync前,我们先看下官方文档:vue .sync 修饰符,里面说vue .sync 修饰符以前存在于vue1.0版本里,但是在在 2.0 中移除了 .sync .但是在 2.0 发 ...

  2. Vue 开发技巧或者说Vue知识点梳理(转,自个学习)

    Vue 组件通讯 ——常见使用场景可以分为三类: 父子通信: 父向子传递数据是通过 props,子向父是通过 events($emit):通过父链 / 子链也可以通信($parent / $child ...

  3. 【Vue】Vue中的父子组件通讯以及使用sync同步父子组件数据

    前言: 之前写过一篇文章<在不同场景下Vue组件间的数据交流>,但现在来看,其中关于“父子组件通信”的介绍仍有诸多缺漏或者不当之处, 正好这几天学习了关于用sync修饰符做父子组件数据双向 ...

  4. vue学习:props,scope,slot,ref,is,slot,sync等知识点

    1.ref :为子组件指定一个索引 ID,给元素或者组件注册引用信息.refs是一个对象,包含所有的ref组件. <div id="parent"> <user- ...

  5. 理解vue 修饰符sync

    也是在vux中看到了这个sync 现在我们来看看vue中的sync 我们先看下官方文档:vue .sync 修饰符,里面说vue .sync 修饰符以前存在于vue1.0版本里,但是在在 2.0 中移 ...

  6. vue 之 .sync 修饰符

    在一些情况下,我们可能会需要对一个 prop (父子组件传递数据的属性) 进行“双向绑定”. 在vue 1.x 中的 .sync 修饰符所提供的功能.当一个子组件改变了一个带 .sync 的prop的 ...

  7. vue中.sync 修饰符

    一直以来,都不太明白.sync的用法,归根结底原因在于,没有仔细阅读“.sync修饰符”. 正好,最近在拿一个项目练手,然后使用了elment-ui,然后在用到dialog的时候,属性visible是 ...

  8. Vue自定义指令,ref ,sync,slot

    一.自定义指令 vue中可以自己设置指令,通过directive来实现,有2种创建方式,一种是局部创建,一种是全局创建. 第一种:局部创建 如果想注册局部指令,组件中也接受一个 directives  ...

  9. Vue sync修饰符的使用

    父子组件传值,父组件可以给子组件传值,但是子组件是不能修改组件提供的值,这里vue提供了sync修饰符,以前父组件点击子组件显示,子组件关闭按钮,父组件再点击子组件就无法让子组件显示.因为子组件点击关 ...

随机推荐

  1. 《Python基础教程读书笔记》

    第1章 快速构造:基础知识 1.2交互式构造器 不强制分号,一行就是一行.可以加上分号 1.4数字和表达式 加.减.乘.除.整除.求余.指数.取反(-例如-2**2,**的优先级比-大) from _ ...

  2. Xamarin XAML语言教程对象元素的声明方式

    Xamarin XAML语言教程对象元素的声明方式 XAML的对象元素的声明有两种形式,分别为包含属性的特性语法形式以及对象元素语法形式.在1.4小节中,我们看到了其中一种对XAML对象元素的声明方式 ...

  3. 手动安装windows的磁盘清理工具

    All you really need to do is copy some files that are already located on your server into specific s ...

  4. docker 如何清理垃圾呢

    应用容器在宿主机上长期运行,应用实例启停容器,会产生大量的停止的容器,无容器使用的数据卷.网络配置,无容器依赖的镜像,这些垃圾日积月累,会影响到宿主机的运行状态,造成机子卡顿等现象.因此,需要对这些宿 ...

  5. ios文件系统文件目录操作

    对于一个运行在iPhone得app,它只能访问自己根目录下得一些文件(所谓sandbox). 一个app发布到iPhone上后,目录结构如下: 1.其中获取 app root 可以用 NSHomeDi ...

  6. 工作总结 mvc 调页面传参数 参数值会一直保存 在这个页面上的

    意思是 两个页面均可以 获取到id 和 goodsType 都可以获取 id goodsType post 的 还多带点属性值 form data 页面上带过去的 (新增 编辑)

  7. 小胖学PHP总结4-----PHP的字符串操作

    1.字符串连接 字符串是通过半角句号"."来连接的.能够把两个或两个以上的字符串连接成一个字符串. 2.去除字符串首尾空格和特殊字符 PHP中提供了trim()函数去除字符串左右两 ...

  8. VS2012+Win7站点公布具体步骤

    VS2012+Win7站点公布详细步骤 本机环境: 本文分三个部分介绍Web项目公布的常规方法,大神级别能够略过,主要是为了方便一些刚開始学习的人. 第一部分:VS2012把项目公布到文件系统. 第二 ...

  9. mdadm

    http://en.wikipedia.org/wiki/Mdadm mdadm From Wikipedia, the free encyclopedia     mdadm Original au ...

  10. Flash文字效果

    flash中增加文本.使用了消除锯齿:可读性消除锯齿.发现不嵌入字体的无法动态改动里面的文字,但嵌入字体的话会造成swf文件过大. 终于还是选择了使用设备字体,并选择了黑体.出了一个问题.文字没有加粗 ...