vue——三種常見的傳值方式,子傳父、父傳子、同級傳值

父組件向子組件傳值

父組件

<template>
  <div>
    父組件:
    <input type="text" v-model="name">
    <br>
    <br>
    <!-- 引入子組件 -->
    <child :inputName="name"></child>
  </div>
</template>
<script>
  import child from './child'
  export default {
    components: {
      child
    },
    data () {
      return {
        name: ''
      }
    }
  }
</script>

子組件

<template>
  <div>
    子組件:
    <span>{{inputName}}</span>
  </div>
</template>
<script>
  export default {
    // 接收父組件的值
    props: {
      inputName: String,
      required: true
    }
  }
</script>

 

子組件向父組件傳值($emit傳值,v-on接受)

在子組件中需要向父組件傳值處使用this.$emit("function",param);   //其中function爲父組件定義函數,param爲需要傳遞參數

在父組件中子組件引用處添加函數v-on:function="function1"; //其中function爲子組件中定義函數,function1爲父組件定義函數--用於接收子組件傳值並進行相應數據處理,可定義爲同一名稱

v-on: 可用 @ 代替 @function="function1" ,@ 爲 v-on:的簡寫

子組件

<template>
  <div>
    子組件:
    <span>{{childValue}}</span>
    <!-- 定義一個子組件傳值的方法 -->
    <input type="button" value="點擊觸發" @click="childClick">
  </div>
</template>
<script>
  export default {
    data () {
      return {
        childValue: '我是子組件的數據'
      }
    },
    methods: {
      childClick () {
        // childByValue是在父組件on監聽的方法
        // 第二個參數this.childValue是需要傳的值
        this.$emit('childByValue', this.childValue)
      }
    }
  }
</script>

父組件

<template>
  <div>
    父組件:
    <span>{{name}}</span>
    <br>
    <br>
    <!-- 引入子組件 定義一個on的方法監聽子組件的狀態-->
    <child v-on:childByValue="childByValue"></child>
  </div>
</template>
<script>
  import child from './child'
  export default {
    components: {
      child
    },
    data () {
      return {
        name: ''
      }
    },
    methods: {
      childByValue: function (childValue) {
        // childValue就是子組件傳過來的值
        this.name = childValue
      }
    }
  }
</script>

子組件傳輸多個參數時:

// 子組件
this.$emit('test',this.param1,this.param2, this.param3)
// 父組件 arguments 是以數組的形式傳入
@test='test(arguments,userDefined)'

同級組件傳值

公共bus.js

//bus.js
import Vue from 'vue'
export default new Vue()

組件A:

<template>
  <div>
    A組件:
    <span>{{elementValue}}</span>
    <input type="button" value="點擊觸發" @click="elementByValueClick">
  </div>
</template>
<script>
  // 引入公共的bug,來做爲中間傳達的工具
  import Bus from './bus.js'
  export default {
    data () {
      return {
        elementValue: 1
      }
    },
    methods: {
      elementByValueClick: function () {
        Bus.$emit('val', this.elementValue)
      }
    }
  }
</script>

組件B:

<template>
  <div>
    B組件:
    <input type="button" value="點擊觸發" @click="getData">
    <span>{{name}}</span>
  </div>
</template>
<script>
  import Bus from './bus.js'
  export default {
    data () {
      return {
        name: 0
      }
    },
    mounted: function () {
      var vm = this
      // 用$on事件來接收參數
      Bus.$on('val', (data) => {
        console.log(data)
        vm.name = data
      })
    },
    methods: {
      getData: function () {
        this.name++
      }
    }
  }
</script>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章