您好,登錄后才能下訂單哦!
本文實(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ì)有所幫助。
免責(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)容。