vue父子組件之間傳值

parent.vue 父組件:

<template>
  <div>
    <h1>父組件</h1>
    <child :parentToChildData="parentData" @childToParentData="getChildData"></child>
  </div>
</template>
 
<script>
import child from '@/components/child'
export default {
  components:{child},
  data () {
    return {
      parentData: '父組件給子組件的數(shù)據(jù)',
      childData: '用來(lái)存放子組件傳回給父組件的數(shù)據(jù)',
    };
  },
  methods: {
    getChildData(childData) {
      console.log(childData);
    }
  }
}
</script> 

child.vue 子組件

<template>
  <div>
    <h1>子組件</h1>
    <p>從父組件傳過(guò)來(lái)的數(shù)據(jù):{{parentToChildData}}</p>
    <button @click="sonClick">點(diǎn)擊按鈕傳數(shù)據(jù)回父組件</button>
  </div>
</template>
<script>
export default {
  props: ['parentToChildData'],
  data () {
    return {
      childData:'返回給父組件的值'
    };
  },
methods:{
    sonClick () {
      this.$emit('childToParentData', this.childData);
    }}
}
</script>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 做項(xiàng)目時(shí),遇到一些公用的部分,我們會(huì)把它寫(xiě)成公用組件,而組件之間一定要有數(shù)據(jù)交互,所以就出現(xiàn)了父子組件之間傳值的需...
    勤能補(bǔ)拙的笨小孩閱讀 517評(píng)論 1 0
  • Vue的組件化給前端開(kāi)發(fā)帶來(lái)極大的便利,這種依賴數(shù)據(jù)來(lái)控制Dom的模式,區(qū)別于以前的開(kāi)發(fā)控制Dom的開(kāi)發(fā)理念,這也...
    Max_Law閱讀 1,199評(píng)論 0 3
  • 3.0的目錄簡(jiǎn)單了很多,少了build,config兩個(gè)目錄。需要對(duì)webpack進(jìn)行配置的話,要手動(dòng)在根目錄新建...
    jiahzhon閱讀 586評(píng)論 0 0
  • 概述 vue中組件之間的傳值傳值情況主要有以下三種 父組件向子組件傳值子組件向父組件傳值兄弟組件之間相互傳值或者是...
    johnnie_wang閱讀 664評(píng)論 2 3
  • 1997年,戴安娜王妃不幸車(chē)禍身亡,世界為之震驚,舉國(guó)悲痛欲絕,但是英國(guó)王室人員卻對(duì)此采取了逃避疏離的態(tài)度。...
    橙小曉閱讀 6,241評(píng)論 0 3

友情鏈接更多精彩內(nèi)容