Vue?關(guān)于$emit與props的使用示例代碼
一、props 和 $emit
1、子組件向父組件傳值,通過$emit 事件向父組件發(fā)送消息,將自己的數(shù)據(jù)傳遞給父組件。
2、props 可以在組件上注冊一些自定義屬性。父組件通過綁定該屬性來向子組件傳入數(shù)據(jù),子組件通過 props 屬性獲取對應(yīng)數(shù)據(jù)。
二、示例
1.父組件
// parent.vue
<template>
<div>
<p>childMessage: {{childMessage}}</p>
<children :sendmessage='childMessage' @showMsg="updataMsg"></children>
</div>
</template>
import children from '/*...*/'
export default{
data () {
return {
childMessage: '父組件給子組件傳值'
}
},
methods: {
updataMsg(message) {
this.childMessage = message
console.log(this.childMessage)
components: {
children
}
}2.子組件
// children.vue
<template>
<div>
// 通過按鈕點擊事件將子組件的值傳給父組件
<button @click="sendtoParent">Click this Button</button>
</div>
</template>
<script>
export default {
props: ['sendmessage'],
methods: {
sendtoParent() {
//$emit括號里的第一個參數(shù)為自定義事件
this.$emit('showMsg','子組件通過$emit給父組件傳值')
}
}
}
</script>
結(jié)果展示
父子組件通信之前

父子組件通信之后

到此這篇關(guān)于Vue 關(guān)于$emit與props的使用的文章就介紹到這了,更多相關(guān)Vue $emit與props的使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue實現(xiàn)Excel預(yù)覽功能使用場景示例詳解
這篇文章主要為大家介紹了Vue實現(xiàn)Excel預(yù)覽功能使用場景示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09
如何在Vue中使localStorage具有響應(yīng)式(思想實驗)
這篇文章主要介紹了如何在Vue中使localStorage具有響應(yīng)式,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07
Vue2+Elementui?Dialog實現(xiàn)封裝自定義彈窗組件
在日常的管理系統(tǒng)界面中,我們寫的最多的除了列表表格之外,就是各種彈窗組件,本文就來為大家詳細介紹一下Vue2如何結(jié)合Elementui?Dialog實現(xiàn)封裝自定義彈窗組件,希望對大家有所幫助2023-12-12
vue在使用element組件出現(xiàn)<el-input>標簽無法輸入的問題
這篇文章主要介紹了vue在使用element組件出現(xiàn)<el-input>標簽無法輸入的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04

