美文网首页
关于v-model以及父子间值同步的使用

关于v-model以及父子间值同步的使用

作者: 刃雪幻城 | 来源:发表于2018-12-27 16:46 被阅读0次

父组件

<template>
    <div id="fat">
        <input type="text" v-model="fatherValue" />
        <!--子传父事件,父传子props-->
        <son @fatAct="sonAct" :fatVal="fatherValue"></son>
    </div>
</template>
<script>
//  引入子组件
import Son from './Son'
export default {
  components:{
    Son
  },
  data(){
    return {
      fatherValue:''
    }
  },
  methods:{
    fatAct: function (childValue) {
        // childValue就是子组件传过来的值
        this.fatherValue = childValue
      }
  }
}
</script>
<style></style>

子组件

<template>
    <div id="son">
        <input type="text" v-model="sonValue" />
    </div>
</template>
<script>
export default {
  props:{
    fatherValue:{
      type:String,
      default:function(){
        return ''
      }
    }
  },
  computed:{
    sonValue:{
      get:function(){
        return this.fatherValue
      },
      set:function(value){
        this.$emit("sonAct",value);
      }
    }
  }
};
</script>
<style></style>

相关文章

网友评论

      本文标题:关于v-model以及父子间值同步的使用

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