首页 > 技术文章 > vue 组件之间的数据传递

stella1024 2017-09-27 20:17 原文

父组件传数据给子组件

  1. 创建数据
  2. 获取json数据

 

子组件传数据给父组件

  1. 子组件使用$emit监听数据

getAreaValues(){
    this.$emit('getAreaValues', {
         provId: this.provModel,
         cityId: this.cityModel,
         districtId : this.districtModel
    })
}

  2. 在父组件接收,在组件标签使用v-on绑定子组件的同名事件 “getAreaValues”

<template>
    <div class="index">
       <area-select v-on:getAreaValues="getValue"></area-select>
    </div>
    
    
</template>

<script>
    import areaSelect from './public/selects/areaSelect'
    export default {
        components: {
            areaSelect
        },
        methods: {
            getValue(areaValues){
                console.log(areaValues);  //组件的返回值
            }
        }
    }
</script>

同级组件之间传数据

  

    

推荐阅读