溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

vue實(shí)現(xiàn)組件之間傳值功能示例

發(fā)布時(shí)間:2020-09-01 00:14:15 來源:腳本之家 閱讀:154 作者:哼著自己的小調(diào)調(diào) 欄目:web開發(fā)

本文實(shí)例講述了vue實(shí)現(xiàn)組件之間傳值功能。分享給大家供大家參考,具體如下:

slot標(biāo)簽:

想向封裝好結(jié)構(gòu)的組件中插入內(nèi)容,需要借助<slot></slot>

在組件之中進(jìn)行關(guān)聯(lián):如

模板中:

<slot name='txt'></slot>

組件調(diào)用中:

<p slot='txt'></p>

注:如果只有slot上面每一定義name屬性,則只能有一個(gè)slot

<div class='box'>
  <com>
    <p slot='txt'></p>
  </com>
</div>
<template id="c">
  <div>
    <slot name="txt"></slot>
  </div>
</template>

Vue.component('com',{
  template:"#c"
})

父組件向子組件傳值props

父組件:

<template>
  <child :parent-msg='a'></child>
</template>

子組件:

child:{
  template:'#chi'
  props:['parentMsg']
}

子組件向父組件的傳值:

vue只運(yùn)行數(shù)據(jù)的單選傳遞,在子組件向父組件的傳值中,需要事件觸發(fā)

子組件:

<template>
  <div @click="up"></div>
</template>

methods:{
 up(){
  this.$emit('fn','msg') // 主動(dòng)觸發(fā)fn方法,msg是需要傳遞的數(shù)據(jù)
 }
}

父組件:

<div>
  <child @fn="getval"></child>
</div>

methods:{
  getval(msg){ // msg接收到的數(shù)據(jù)
    this.msg=msg
  }
}

希望本文所述對(duì)大家vue.js程序設(shè)計(jì)有所幫助。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI