zoukankan      html  css  js  c++  java
  • vue父组件与子组件之间的数据传递

    父组件向子组件传递数据

    父组件用数据绑定;子组件用props接收

    <!-- test-vue-model父组件 -->
    <template>
      <div>
        <model-son :datas="fatherData"></model-son>
      </div>
    </template>
    <script>
    import ModelSon from 'views/test/test-vue-model-son'
    export default{
      data() {
        return {
          fatherData: '父传子'
        }
      },
      components: {
        ModelSon
      }
    }
    </script>
    <!-- test-vue-model-son  子组件 -->
    <template>
      <div><span>子组件</span>{{datas}}</div>
    </template>
    <script>
    export default{
      props: {
        datas: {
          type: String,
          default: ''
        }
      }
    }
    </script>

    子组件向父组件传递参数通过事件发送

    <!-- test-vue-model父组件 -->
    <template>
      <div>
        <model-son :datas="fatherData" @receiveMe="handleMe"></model-son>
      </div>
    </template>
    <script>
    import ModelSon from 'views/test/test-vue-model-son'
    export default{
      data() {
        return {
          fatherData: '父传子'
        }
      },
      methods: {
        handleMe(item) {
          console.log('接收子元素item', item)
        }
      },
      components: {
        ModelSon
      }
    }
    </script>
    <!-- test-vue-model-son  子组件 -->
    <template>
      <div>
        <div><span>子组件</span>{{datas}}</div>
        <div @click="emitNewData()">向父组件发送事件</div>
      </div>
    </template>
    <script>
    export default{
      props: {
        datas: {
          type: String,
          default: ''
        }
      },
      data() {
        return {
          new: '我的'
        }
      },
      methods: {
        emitNewData() {
          this.$emit('receiveMe', this.new)
        }
      }
    }
    </script>

    绿色为:父传子路径

    红色:子传父路径

  • 相关阅读:
    insertAfter()
    Unity3D之协程(Coroutines & Yield )
    C#中 As 和强制转换的总结
    Mesh系列文章
    在Unity3D 4中关联Visual Studio 2012来编写C#
    Unity3D安装多版本
    Time.deltaTime 增量时间
    Unity3D中Update和Lateupdate的区别
    Making raycast ignore multiple layers
    Unity3d中SendMessage 用法简单笔记
  • 原文地址:https://www.cnblogs.com/beka/p/8631878.html
Copyright © 2011-2022 走看看