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

前言

父传子

由父组件向子组件传递数据,我们需要以下的几个步骤

1. 引入子组件,并且挂载

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

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

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

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

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

子传父

父子传递“双向绑定”

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

vue中有一个很神奇的东西叫 v-model,它可以完成我们的需求。

使用v-model过程中,父组件我们还是需要将子组件正常引入,只是传值方式改成了v-model

父组件

<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>

子组件

<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的值赋值到自己的变量,修改自己的变量是可以的。

《vue中使用v-model完成组件间的通信》

子组件 – 修改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);
    }
  }
};
    原文作者:麦篱落
    原文地址: https://segmentfault.com/a/1190000020145084
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞