Vue中如何實(shí)現(xiàn)父子組件傳值

Vue中如何實(shí)現(xiàn)父子組件傳值,相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。

建網(wǎng)站原本是網(wǎng)站策劃師、網(wǎng)絡(luò)程序員、網(wǎng)頁設(shè)計(jì)師等,應(yīng)用各種網(wǎng)絡(luò)程序開發(fā)技術(shù)和網(wǎng)頁設(shè)計(jì)技術(shù)配合操作的協(xié)同工作。成都創(chuàng)新互聯(lián)專業(yè)提供網(wǎng)站制作、成都做網(wǎng)站,網(wǎng)頁設(shè)計(jì),網(wǎng)站制作(企業(yè)站、響應(yīng)式網(wǎng)站、電商門戶網(wǎng)站)等服務(wù),從網(wǎng)站深度策劃、搜索引擎友好度優(yōu)化到用戶體驗(yàn)的提升,我們力求做到極致!

(一)傳遞數(shù)值

1.子組件:Header.vue

<template>
 <div>
  <!-- data對(duì)象里并沒有 msg 屬性,這里調(diào)用的是父類傳遞過來的 msg 屬性 -->
  <h3>{{msg}}</h3>
 </div>
</template>
<script>
export default {
 data() {
  return {
  }
 },
 methods: {
 },
 // 接收父類的傳值
 props: ['msg']
}
</script>

可以看到,在子組件中的data對(duì)象里并沒有 msg 屬性,這里調(diào)用的是父類傳遞過來的 msg 屬性,接收就是靠 props: ['msg']。

2.父組件Home.vue

<template>
 <div>
  <!-- 2.使用子組件,并向子組件傳值 -->
  <v-head :msg="msg"></v-head>

  <br>
  <br>
 </div>
</template>

<script>
// 1.引入子組件
import Head from './Head.vue';

export default {
 data() {
  return {
   msg: '我是一個(gè)組件'
  }
 },
 methods: {
 },
 components: {
  "v-head": Head
 },
 // 頁面刷新時(shí)請(qǐng)求數(shù)據(jù)
 mounted() {
 }
}
</script>

傳值的核心思想就是,在使用子組件的地方,加上要傳遞的值:<v-head :msg="msg"></v-head>

(二)傳遞方法

傳遞方法的寫法和傳遞數(shù)值一樣,下面只寫出關(guān)鍵步驟:

父組件

<template>
 <div>
  <!-- 2.使用子組件,并向子組件傳值 -->
  <v-head :run="run"></v-head>

  <br>
  <br>
 </div>
</template>

<script>
// 1.引入子組件
import Head from './Head.vue';

export default {
 data() {
  return {
   msg: '我是一個(gè)組件'
  }
 },
 methods: {
  run() {
   alert(this.msg);
  }
 },
 components: {
  "v-head": Head
 },
 // 頁面刷新時(shí)請(qǐng)求數(shù)據(jù)
 mounted() {
 }
}
</script>

子組件

<template>
 <div>
  <button @click="run">接收父組件的方法</button>
 </div>
</template>
<script>
export default {
 data() {
  return {
  }
 },
 methods: {
 },
 // 接收父類的傳值
 props: ['run']
}
</script>

(三)傳遞對(duì)象

傳遞對(duì)象的寫法和傳遞數(shù)值一樣,下面只寫出關(guān)鍵步驟:

父組件

<template>
 <div>
  <!-- 2.使用子組件,并向子組件傳值,這里的 this 就是 Home 組件 -->
  <v-head :home="this"></v-head>

  <br>
  <br>
 </div>
</template>

<script>
// 1.引入子組件
import Head from './Head.vue';

export default {
 data() {
  return {
   msg: '我是一個(gè)組件'
  }
 },
 methods: {
  run() {
   alert(this.msg);
  }
 },
 components: {
  "v-head": Head
 },
 // 頁面刷新時(shí)請(qǐng)求數(shù)據(jù)
 mounted() {
 }
}
</script>

子組件

<template>
 <div>
  <!-- data對(duì)象里并沒有 msg 屬性,這里調(diào)用的是父類傳遞過來的 msg 屬性 -->
  <h3>{{msg}}</h3>
  <br>
  <br>
  <button @click="run">接收父組件的方法</button>
 </div>
</template>
<script>
export default {
 data() {
  return {
   // 調(diào)用傳過來的home組件的msg屬性
   msg: this.home.msg
  }
 },
 methods: {
  run() {
   // 調(diào)用傳過來的home組件的run()方法
   this.home.run();
  }
 },
 // 接收父類的傳值
 props: ['home']
}
</script>

(四)傳遞數(shù)值類型校驗(yàn)

props: {
  'home': Object
 }

看完上述內(nèi)容,你們掌握Vue中如何實(shí)現(xiàn)父子組件傳值的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

當(dāng)前文章:Vue中如何實(shí)現(xiàn)父子組件傳值
鏈接分享:http://bm7419.com/article40/pceieo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、網(wǎng)站設(shè)計(jì)、網(wǎng)站導(dǎo)航、品牌網(wǎng)站建設(shè)品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站收錄

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)