美文网首页
vue 父子组件传值 $on sync v-model

vue 父子组件传值 $on sync v-model

作者: Devi_l | 来源:发表于2019-10-17 18:25 被阅读0次

1、正常的父子组件传值
2、使用sync 实现父子组件传值
3、使用v-model实现父子组件传值

<div id="app">
</div>
//正常 emit传值
Vue.component('my-component', {
  template: `<div>
        {{msg}}:<my-counter :cont="msg" @add="change" /> 
  </div>`,
  data:function(){
    return {
      msg:1212
    }
  },
  methods: {
    change(val){
        this.msg = val;
    }
  }
});
Vue.component("my-counter", {
  template: `<div>
    <button @click="add">++</button>
  </div>`,
  props: ['cont'],
  methods: {
    add(){
        this.$emit('add',this.cont+1)
    }
  }
});
new Vue({
    el: '#app'
})
// 通过.aync  
Vue.component('my-component', {
  template: `<div>
        {{msg}}:<my-counter :cont.sync="msg" /> 
  </div>`,
  data:function(){
    return {
      msg:1212
    }
  },
  methods: {
    change(val){
        this.msg = val;
    }
  }
});
Vue.component("my-counter", {
  template: `<div>
    <button @click="add">{{cont}}++</button>
  </div>`,
  props: ['cont'],
  methods: {
    add(){
        this.$emit('update:cont',this.cont+1)
    }
  }
});
new Vue({
    el: '#app',
    template:"<my-component />"
})



//通过v-model 第一种
Vue.component('my-component', {
  template: `<div>
        {{msg}}:<my-counter v-model="msg" /> 
  </div>`,
  data:function(){
    return {
      msg:1212
    }
  },
  methods: {
    change(val){
        this.msg = val;
    }
  }
});
Vue.component("my-counter", {
  template: `<div>
    <button @click="add">{{msg}}++</button>
  </div>`,
  model:{
    prop:'msg',
event:'change'
  },
  props: ['msg'],
  methods: {
    add(){
        this.$emit('change',this.msg+1)
    }
  }
});
new Vue({
    el: '#app',
    template:"<my-component />"
})
//通过v-model 第二种 写在最后推荐
Vue.component('my-component', {
  template: `<div>
        {{msg}}:<my-counter v-model="msg" /> 
  </div>`,
  data:function(){
    return {
      msg:1212
    }
  },
  methods: {
    change(val){
        this.msg = val;
    }
  }
});
Vue.component("my-counter", {
  template: `<div>
    <button @click="add">{{value}}++</button>
  </div>`,
  props: ['value'],
  methods: {
    add(){
        this.$emit('input',this.value+1)
    }
  }
});
new Vue({
    el: '#app',
    template:"<my-component />"
})

相关文章

  • vue 父子组件传值 $on sync v-model

    1、正常的父子组件传值2、使用sync 实现父子组件传值3、使用v-model实现父子组件传值

  • (VUE3) 四、组件传值(父子组件传值 & 祖孙组件传值 &v

    1.父子组件传值 vue2中的父子组件传值:父组件: 子组件: vue3中的父子组件传值: 还是用props接收父...

  • Vue父子组件通信和双向绑定

    本篇文章主要介绍父子组件传值,组件的数据双向绑定。 1. 基础父子组件传值 父子组件传值,这是Vue组件传值最常见...

  • VUE组件(传值,生命周期)

    VUE生命周期 VUE子传父组件通信 VUE非父子组件传值

  • 组件通信

    vue传值可分为父子之间传值、兄弟组件之间传值、跨代组件之间传值 1.父子之间传值:可以使用$emit/props...

  • Vue组件之间的传值

    Vue父子组件之间的传值(props)兄弟组件 VUEX

  • 前端基础搬运工-VUE模块

    十、VUE模块 基础部分 1. Vue组件间传值 答: -[ ] 1.父子之间的传值 父组件向子组件传值通过p...

  • .sync原理(Vue组件父子传值)

    使用前提: 首先,我们需要明确的是,子父组件之间通讯,子组件是不能直接改变父组件的值的。(父组件是大佬,小弟不能改...

  • 2019-03-13

    vue父子组件传值,(父组件向子组件传值用prop ,子组件向父组件传值:子组件调用父组件方法值以参数的方式传递)...

  • 2019-03-13

    vue父子组件传值,(父组件向子组件传值用prop ,子组件向父组件传值:子组件调用父组件方法值以参数的方式传递)...

网友评论

      本文标题:vue 父子组件传值 $on sync v-model

      本文链接:https://www.haomeiwen.com/subject/vtrvmctx.html