vue .sync修饰符的使用详解
vue的官网介绍非常不错,先通读一遍。
2.3.0+ 新增
在有些情况下,我们可能需要对一个 prop 进行“双向绑定”。不幸的是,真正的双向绑定会带来维护上的问题,因为子组件可以修改父组件,且在父组件和子组件都没有明显的改动来源。
这也是为什么我们推荐以 update:my-prop-name
的模式触发事件取而代之。举个例子,在一个包含 title prop 的假设的组件中,我们可以用以下方法表达对其赋新值的意图:
this.$emit('update:title', newTitle)
然后父组件可以监听那个事件并根据需要更新一个本地的数据属性。例如:
<text-document v-bind:title="doc.title" v-on:update:title="doc.title = $event" ></text-document>
为了方便起见,我们为这种模式提供一个缩写,即 .sync 修饰符:
<text-document v-bind:title.sync="doc.title"></text-document>
当我们用一个对象同时设置多个 prop 的时候,也可以将这个 .sync 修饰符和 v-bind 配合使用:
<text-document v-bind.sync="doc"></text-document>
这样会把 doc 对象中的每一个属性 (如 title ) 都作为一个独立的 prop 传进去,然后各自添加用于更新的 v-on 监听器。
以下为代码实例
father.vue
<template> <div class="hello"> //input实时改变wrd的值, 并且会实时改变box里的内容 <input type="text" v-model="wrd"> <box :word.sync="wrd" ></box> </div> </template> <script> import box from './box' //引入box子组件 export default { name: 'HelloWorld', data() { return { wrd: '' } }, components: { box } } </script> child.vue <template> <div class="hello"> <div class="ipt"> <input type="text" v-model="str"> </div> //word是父元素传过来的 <h2>{{ word }}</h2> </div> </template> <script> export default { name: 'box', data() { return { str: '', } }, props: { word: '' }, watch: { str: function(newValue, oldValue) { //每当str的值改变则发送事件update:word , 并且把值传过去 this.$emit('update:word', newValue) } } } </script>
那这个修饰符的原理是什么呢?其实还是vue父组件可以监听子组件的事件,自组件可以触发父组件的事件
father.vue
<template> <div class="hello"> <input type="text" v-model="wrd"> <box @incre="boxIncremend" ></box> </div> </template> <script> import box from './box' export default { name: 'HelloWorld', data() { return { wrd: '' } }, methods: { boxIncremend(e) { this.wrd = this.wrd + e } }, components: { box } } </script> child.vue <template> <div class="hello"> <input type="text" v-model="str"> <h2>{{ word }}</h2> </div> </template> <script> export default { name: 'box', data() { return { num: 0 } }, props: { word: '' }, watch: { str: function(neww, old) { //往父级发射incre事件 this.$emit('incre', ++this.num) } }, } </script>
在没有sync的时候,我们要实现双向绑定,可能需要在父组件里绑定一个事件和一个值
father.vue
<template> <div class="hello"> <input type="text" v-model="wrd"> <box @incre="boxIncremend" :word="word"></box> </div> </template> <script> import box from './box' export default { name: 'HelloWorld', data() { return { word: '' } }, methods: { boxIncremend(newword) { this.word = newword } }, components: { box } } </script> child.vue <template> <div class="hello"> <input type="text" v-model="str"> <h2>{{ word }}</h2> </div> </template> <script> export default { name: 'box', props: { word: '' }, watch: { str: function(newword) { //往父级发射incre事件 this.$emit('incre', newword) } }, } </script>
但是有了sync之后,我们就可以这么写
father.vue
<template> <div class="hello"> <input type="text" v-model="wrd"> <box :wrd.sync="wrd"></box> </div> </template> <script> import box from './box' export default { name: 'HelloWorld', data() { return { wrd: '' } }, methods: { boxIncremend(e) { this.wrd = e } }, components: { box } } </script>
child.vue
<template> <div class="hello"> <input type="text" v-model="str"> <h2>{{ word }}</h2> </div> </template> <script> export default { name: 'box', props: { word: '' }, watch: { str: function(newword) { //往父级发射incre事件 this.$emit('update:word', newword) } }, } </script>
父组件中的子组件,少写了一个自定义事件属性,子组件中$emit直接出发父组件中数据的更新,清新明了。使用中需要注意的是,update和后面对应的数据名不能写错。
父子组件的双向数据绑定
父组件改变数据可以改变子组件, 但是子组件的内容改变并不会影响到父组件
可以通过2.3.0新增的sync修饰符来达到双向绑定的效果
father.vue
<template> <div class="hello"> //input实时改变wrd的值, 并且会实时改变box里的内容 <input type="text" v-model="wrd"> <box :word.sync="wrd" ></box> </div> </template> <script> import box from './box' //引入box子组件 export default { name: 'HelloWorld', data() { return { wrd: '' } }, components: { box } } </script> <style scoped></style>
box.vue
<template> <div class="hello"> <div class="ipt"> <input type="text" v-model="str"> </div> //word是父元素传过来的 <h2>{{ word }}</h2> </div> </template> <script> export default { name: 'box', data() { return { str: '', } }, props: { word: '' }, watch: { str: function(newValue, oldValue) { //每当str的值改变则发送事件update:word , 并且把值传过去 this.$emit('update:word', newValue) } } } </script> <style scoped></style>
利用了父级可以在子元素上监听子元素的事件
father.vue
<template> <div class="hello"> <input type="text" v-model="wrd"> <box @incre="boxIncremend" ></box> </div> </template> <script> import box from './box' export default { name: 'HelloWorld', data() { return { wrd: '' } }, methods: { boxIncremend(e) { this.wrd = this.wrd + e } }, components: { box } } </script> <style scoped></style>
box.vue
<template> <div class="hello"> <input type="text" v-model="str"> <h2>{{ word }}</h2> </div> </template> <script> export default { name: 'box', data() { return { num: 0 } }, props: { word: '' }, watch: { str: function(neww, old) { //往父级发射incre事件 this.$emit('incre', ++this.num) } }, } </script> <style scoped></style>
总结
以上所述是小编给大家介绍的vue .sync修饰符的使用,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
- Go语言使用sort包对任意类型元素的集合进行排序的方法
- TiDB 源码阅读系列文章(五)TiDB SQL Parser 的实现
- [go语言]吐槽:怎么样实现支持并发访问的数据集合更好?
- 利用缓冲信道来实现网游帐号验证消息的分发和等待
- Buffered channel、slice和mutex的简单性能测试
- 原子读写性能简单测试
- 从小白变RSA大神,附常用工具使用方法及CTF中RSA典型例题
- select多路选择的模拟实现
- 初学者需要注意的问题-变量的作用域
- 在 Go 语言中,正确的使用并发
- Injectify:一款执行MiTM攻击的工具
- 看我如何发现Google云平台漏洞并获得$7500赏金
- Go语言写Web 应用程序
- 小萝莉说Crash(一):Unrecognized selector sent to instance xxxx
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- JavaScript 技术篇-chrome利用ClipboardEvent写入剪切板,没成功的你就差一步
- 搭建一个高可用负载均衡的集群架构(第三部分)
- C语言学习笔记
- JavaScript 技术篇-js获取窗口标题名,获取页面URL地址
- Pyhon海龟绘制木叶村徽章
- MIT大神写给女神的Q版Python画图库—Cutecharts【技术创作101训练营】
- JavaScript 技术篇-js创建dom节点,并设置属性
- 容器中的数据管理
- Java基础 方法
- Python 技术篇-pyperclip库实现读取写入剪切板,超简单
- 基于consul的Docker-overlay跨多宿主机容器网络
- Python 微信机器人-向指定名称的好友发送微信消息
- JavaScript技术篇-js提升网页视频播放速率,提高慕课网视频播放速度
- 使用docker五步搭建ELK日志收集分析系统
- HDFS之SequenceFile和MapFile