vue中使用v-model完成组件间的通讯

前言

父传子

由父组件向子组件传递数据,咱们须要如下的几个步骤vue

1. 引入子组件,而且挂载数组

import Child from './Child'
//components属性中挂载
components:{
    Child
}

2. 将子组件做为标签名,并向子组件传递数据函数

<Child message="向子组件传递的消息"></Child>

3. 子组件使用props接收,父组件传递的数据 this

props:["message"],
   data(){
       return {
           msg:this.message
       }
   }

子传父

父子传递“双向绑定”

以上的两种方法,都是实现的单向数组传递,那如何实现两个组件之间的双向传递呢?
即,在父组件中修改了值,子组件会当即更新。
在子组件中修改了值,父组件中当即更新。spa

vue中有一个很神奇的东西叫 v-model,它能够完成咱们的需求。双向绑定

使用v-model过程当中,父组件咱们仍是须要将子组件正常引入,只是传值方式改为了v-modelcode

父组件component

<template>
  <div>
    {{fatherText}}
    <Child v-model="fatherText"></Child>//调用子组件,并将 fatherText传递给子组件
    <button @click="changeChild">changeChildButton</button>
  </div>
</template>

<script>
import Child from "./Child.vue";
export default {
  name: "father",
  data() {
    return {
      fatherText: "i'm fathertext"
    };
  },
  components: {
    Child
  },
  methods: {
    changeChild() {
      this.fatherText = "father change the text";
    }
  }
};
</script>

子组件blog

<template>
  <div>
   <p class="child" @click="change">{{fatherText}}</p>//正常使用fatherText的值,并添加一个修改值 的方法
  </div>
</template>
<script>
export default {
  name: "child",
  model: {//添加了model方法,用于接收v-model传递的参数
    prop: "fatherText", //父组件中变量的传递
    event: "changeChild" //事件传递
  },
  props: {
    fatherText: {//正常使用props接收fatherText的值
      type: String
    }
  },
  data() {
    return {
     
    };
  },
  methods: {
      change(){
          this.fatherText = 'son change the text'
      }
  }
};
</script>

在这里,报了一个错误,这是由于props数据流是单向的,子组件不该该直接修改props里的值。
因此咱们须要迂回着修改,在子组件中定义一个本身的变量,再将props的值赋值到本身的变量,修改本身的变量是能够的。事件

clipboard.png

子组件 - 修改props中的值

<template>
  <div>
    <p class="child" @click="change">{{childText}}</p>
  </div>
</template>
<script>
export default {
  name: "child",
    model: {
    prop: "fatherText", //父组件中变量的传递
    event: "changeChild" //事件传递
  },
  props: {
    fatherText: {
      type: String
    }
  },
  data() {
    return {
      childText: this.fatherText //定义本身的变量childText
    };
  },
  methods: {
    change() {
      this.childText = "son change the test";//修改本身的变量
    }
  }
};

两个组件间更新
完成了上述代码,你会发现两个组件都改变的内容,可是只更新了自身组件的内容,如何使两个组件进行同步更新呢?

这里须要使用到Wath方法,在子组件中添加wacth方法,监听两个属性的变化

父组件动态改变子组件的值
使用父组件传递的变量做为方法名,以下
父组件传递的变量名为fatherText,因此watch的方法名也为 fatherText(newText),以此来达到监听的效果
其中newText为父组件改变后的新值(能够自定义其余变量名),咱们能够直接调用。而后将新值赋值给子组件的变量,从而完成子组件变量的更新

子组件动态改变父组件的值
若是想使子组件中变量更新,父组件也同时更新。一样,须要监听子组件的变量变量,以子组件的变量名为方法名
使用$emit向父组件通讯,通知父组件变量改变。

<template>
  <div>
    <p class="child" @click="changeChild">{{childText}}</p>
  </div>
</template>
<script>
export default {
  name: "child",
  model: {
    prop: "fatherText", //父组件中变量的传递
    event: "changeChild" //事件传递
  },
  props: {
    fatherText: {
      type: String
    }
  },
  data() {
    return {
      childText: this.fatherText
    };
  },
  methods: {
    changeChild() {
      this.childText = "son change the test";
    }
  },
  watch: {
    fatherText(newtext) {//使用父组件中变量名为函数名,监听fatherText的变化,若是变化,则改变子组件中的值
      this.childText = newtext;
    },
    childText(newtext) {//监听子组件中childText变化,若是变化,则通知父组件,进行更新
      this.$emit("changeChild", newtext);
    }
  }
};
相关文章
相关标签/搜索