Understand .sync in Vue
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:
'<button @click="add">You clicked me {{ childCount }} times.</button>',
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:
'<button @click="add">You clicked me {{ childCount }} times.</button>',
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:
'<button @click="add">You clicked me {{ childCount }} times.</button>',
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
原文地址:https://segmentfault.com/a/1190000017107941
Understand .sync in Vue的更多相关文章
- 深入理解vue 修饰符sync【 vue sync修饰符示例】
在说vue 修饰符sync前,我们先看下官方文档:vue .sync 修饰符,里面说vue .sync 修饰符以前存在于vue1.0版本里,但是在在 2.0 中移除了 .sync .但是在 2.0 发 ...
- Vue 开发技巧或者说Vue知识点梳理(转,自个学习)
Vue 组件通讯 ——常见使用场景可以分为三类: 父子通信: 父向子传递数据是通过 props,子向父是通过 events($emit):通过父链 / 子链也可以通信($parent / $child ...
- 【Vue】Vue中的父子组件通讯以及使用sync同步父子组件数据
前言: 之前写过一篇文章<在不同场景下Vue组件间的数据交流>,但现在来看,其中关于“父子组件通信”的介绍仍有诸多缺漏或者不当之处, 正好这几天学习了关于用sync修饰符做父子组件数据双向 ...
- vue学习:props,scope,slot,ref,is,slot,sync等知识点
1.ref :为子组件指定一个索引 ID,给元素或者组件注册引用信息.refs是一个对象,包含所有的ref组件. <div id="parent"> <user- ...
- 理解vue 修饰符sync
也是在vux中看到了这个sync 现在我们来看看vue中的sync 我们先看下官方文档:vue .sync 修饰符,里面说vue .sync 修饰符以前存在于vue1.0版本里,但是在在 2.0 中移 ...
- vue 之 .sync 修饰符
在一些情况下,我们可能会需要对一个 prop (父子组件传递数据的属性) 进行“双向绑定”. 在vue 1.x 中的 .sync 修饰符所提供的功能.当一个子组件改变了一个带 .sync 的prop的 ...
- vue中.sync 修饰符
一直以来,都不太明白.sync的用法,归根结底原因在于,没有仔细阅读“.sync修饰符”. 正好,最近在拿一个项目练手,然后使用了elment-ui,然后在用到dialog的时候,属性visible是 ...
- Vue自定义指令,ref ,sync,slot
一.自定义指令 vue中可以自己设置指令,通过directive来实现,有2种创建方式,一种是局部创建,一种是全局创建. 第一种:局部创建 如果想注册局部指令,组件中也接受一个 directives ...
- Vue sync修饰符的使用
父子组件传值,父组件可以给子组件传值,但是子组件是不能修改组件提供的值,这里vue提供了sync修饰符,以前父组件点击子组件显示,子组件关闭按钮,父组件再点击子组件就无法让子组件显示.因为子组件点击关 ...
随机推荐
- 洛谷—— P2117 小Z的矩阵
https://www.luogu.org/problemnew/show/2117 题目描述 小Z最近迷上了矩阵,他定义了一个对于一种特殊矩阵的特征函数G.对于N*N的矩阵A,A的所有元素均为0或1 ...
- Careercup | Chapter 5
5.1 You are given two 32-bit numbers, N andM, and two bit positions, i and j. Write a method to inse ...
- Java使用QRCode.jar生成与解析二维码
原文V:http://www.cnblogs.com/bigroc/p/7496995.html#3797682 正题:Java使用QRCode.jar生成与解析二维码demo 欢迎新手共勉,大神监督 ...
- 如果当前地图文档中有独立的Table,通过Engine如何获取该Table?
将IMap转为ITableCollection,通过ITableCollection.get_Table(int index);来获取该Table
- android ListView详解(转)
在android开发中ListView是比较常用的组件,它以列表的形式展示具体内容,并且能够根据数据的长度自适应显示.抽空把对ListView的使用做了整理,并写了个小例子,如下图. 列表的显示需要三 ...
- 【Maven jar】打包单个或多个文件,有依赖jar包的将架包一起打包成一个jar包供别的项目引用
之前有一片文章,是打包单个java文件的.这次想要将http://www.cnblogs.com/sxdcgaq8080/p/8398780.html 打包成jar包,发现这个java文件中引用了多 ...
- 设置NSZombieEnabled和MallocStackLogging
在XCode.4以上版本号中,设置NSZombieEnabled和MallocStackLogging 1.点击XCode的Product菜单.选择Edit Scheme...选项 2.选择左側的Ru ...
- c++程序猿经典面试题(2)
1.以下程序的输出结果是? #include<stdio.h> main(){ int b=3; int arr[]={6,7,8,9,10}; int *ptr=arr; *(ptr++ ...
- nagios插件之登陆SBC监控电话数
运行:sbc_calls_status_new auto_ssh_sbc_10_17.sh | auto_ssh_sbc_11_17.sh vi sbc_calls_status_new.c #inc ...
- python正则中的贪婪与非贪婪
当重复一个正则表达式时,如用 a*,操作结果是尽可能多地匹配模式.当你试着匹配一对对称的定界符,如 HTML 标志中的尖括号.匹配单个 HTML 标志的模式不能正常工作,因为 .* 的本质是“贪婪”的 ...