码迷,mamicode.com
首页 > 其他好文 > 详细

VUE2.0组件自定义事件.sync实例

时间:2017-10-21 19:04:09      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:function   nod   2.0   技术分享   双向   sync   更改   dom   method   

     
        <div id="root">
          <parent></parent>
       </div>
     var childNode = {
            template:                <div class="child">                    <div>子组件数据 {{ childMsg }} </div>                    <input v-model="childMsg">                    <button v-on:click="add">+1</button>\ //第一步:点击按钮
                </div>            ,
            data:function(){
                return {
                    childMsg:0
                }
            },
            methods:{
                add:function(){ //第二步:触发add方法,再触发update:foo,用childMsg更新foo,因为.sync的原因,数据变成了双向绑定 更改foo同样也更改了父组件的msg属性
                    this.childMsg++
                    this.$emit(‘update:foo‘,this.childMsg)
                }
            }
        }
        var parentNode = {
            template:                <div class="parent">                    <p>父组件数据 {{ msg }} </p>                    <child :foo.sync="msg"></child>\ //(v-bind:foo 给子组件绑定一个foo属性,把父组件的属性msg赋值子组件的foo)
                </div>            ,
            components:{
                ‘child‘:childNode
            },
            data:function(){
                return {
                    msg:0
                }
            }
        }
        new Vue({
            el:‘#root‘,
            components:{
                ‘parent‘:parentNode
            }
        })
DOM渲染的结果为:
技术分享

 

VUE2.0组件自定义事件.sync实例

标签:function   nod   2.0   技术分享   双向   sync   更改   dom   method   

原文地址:http://www.cnblogs.com/ItIsInteresting/p/7705282.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!