這篇文章給大家介紹vue.js中怎么使用$refs和$emit 實(shí)現(xiàn)父子組件交互,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),南豐企業(yè)網(wǎng)站建設(shè),南豐品牌網(wǎng)站建設(shè),網(wǎng)站定制,南豐網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,南豐網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
<strong>父調(diào)子 $refs (把父組件的數(shù)據(jù)傳給子組件) </strong><br><br><template>
<div id="app">
<input type="button" name="" id="" @click="parentCall" value="父調(diào)子" />
<hello ref="chil" />//hello組件
</div>
</template>
<script>
import hello from './components/Hello'
export default {
name: 'app',
'components': {
hello
},
methods: {
parentCall () {
this.$refs.chil.chilFn('我是父元素傳過(guò)來(lái)的')
}
}
}
</script>
/*Hello.vue :*/
<template>
<div class="hello"></div>
</template>
<script>
export default {
name: 'hello',
'methods': {
chilFn (msg) {
alert(msg)
}
}
}
</script><strong>子調(diào)父 $emit (把子組件的數(shù)據(jù)傳給父組件)</strong>
//ps:App.vue 父組件
//Hello.vue 子組件
<!--App.vue :-->
<template>
<div id="app">
<hello @newNodeEvent="parentLisen" />
</div>
</template>
<script>
import hello from './components/Hello'
export default {
name: 'app',
'components': {
hello
},
methods: {
parentLisen(evtValue) {
//evtValue 是子組件傳過(guò)來(lái)的值
alert(evtValue)
}
}
}
</script>
<!--Hello.vue :-->
<template>
<div class="hello">
<input type="button" name="" id="" @click="chilCall()" value="子調(diào)父" />
</div>
</template>
<script>
export default {
name: 'hello',
'methods': {
chilCall(pars) {
this.$emit('newNodeEvent', '我是子元素傳過(guò)來(lái)的')
}
}
}
</script>關(guān)于vue.js中怎么使用$refs和$emit 實(shí)現(xiàn)父子組件交互就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
分享題目:vue.js中怎么使用$refs和$emit實(shí)現(xiàn)父子組件交互
網(wǎng)站網(wǎng)址:http://chinadenli.net/article12/gdpodc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)、域名注冊(cè)、標(biāo)簽優(yōu)化、網(wǎng)站排名、網(wǎng)站設(shè)計(jì)、自適應(yīng)網(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í)需注明來(lái)源: 創(chuàng)新互聯(lián)