Vue?關于$emit與props的使用示例代碼
一、props 和 $emit
1、子組件向父組件傳值,通過$emit 事件向父組件發(fā)送消息,將自己的數(shù)據(jù)傳遞給父組件。
2、props 可以在組件上注冊一些自定義屬性。父組件通過綁定該屬性來向子組件傳入數(shù)據(jù),子組件通過 props 屬性獲取對應數(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>
結果展示
父子組件通信之前
父子組件通信之后
到此這篇關于Vue 關于$emit與props的使用的文章就介紹到這了,更多相關Vue $emit與props的使用內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
如何在Vue中使localStorage具有響應式(思想實驗)
這篇文章主要介紹了如何在Vue中使localStorage具有響應式,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07Vue2+Elementui?Dialog實現(xiàn)封裝自定義彈窗組件
在日常的管理系統(tǒng)界面中,我們寫的最多的除了列表表格之外,就是各種彈窗組件,本文就來為大家詳細介紹一下Vue2如何結合Elementui?Dialog實現(xiàn)封裝自定義彈窗組件,希望對大家有所幫助2023-12-12vue在使用element組件出現(xiàn)<el-input>標簽無法輸入的問題
這篇文章主要介紹了vue在使用element組件出現(xiàn)<el-input>標簽無法輸入的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04