vue的官网介绍很是不错,先通读一遍。javascript
2.3.0+ 新增vue
在有些状况下,咱们可能须要对一个 prop 进行“双向绑定”。不幸的是,真正的双向绑定会带来维护上的问题,由于子组件能够修改父组件,且在父组件和子组件都没有明显的改动来源。java
这也是为何咱们推荐以 update:my-prop-name
的模式触发事件取而代之。举个例子,在一个包含 title
prop 的假设的组件中,咱们能够用如下方法表达对其赋新值的意图:web
this.$emit('update:title', newTitle) |
而后父组件能够监听那个事件并根据须要更新一个本地的数据属性。例如:app
<text-document |
为了方便起见,咱们为这种模式提供一个缩写,即 .sync
修饰符:this
<text-document v-bind:title.sync="doc.title"></text-document> |
当咱们用一个对象同时设置多个 prop 的时候,也能够将这个 .sync
修饰符和 v-bind
配合使用:spa
<text-document v-bind.sync="doc"></text-document> |
这样会把 doc
对象中的每个属性 (如 title
) 都做为一个独立的 prop 传进去,而后各自添加用于更新的 v-on
监听器。双向绑定
如下为代码实例code
father.vuecomponent
<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和后面对应的数据名不能写错。
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>
<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>