Vue中子組件向父組件傳值$emit、.sync的案例詳解
首先我們可先了解一個父組件向子組件傳值的一個案例:將父組件請求的后端數(shù)據(jù)傳值給子組件props
因為通過屬性傳值是單向的,有時候我們需要子組件的data 數(shù)據(jù)需要交給父組件使用:通過在子組件上定義自定義事件,在子組件中通過$emit 來觸發(fā)事件;子組件的事件被觸發(fā)并傳參,事件處理函數(shù)可以接收到子組件的數(shù)據(jù);事件綁定的事件處理函數(shù)在父節(jié)點上,故可在事件處理函數(shù)中用到子組件的數(shù)據(jù)值來修改父節(jié)點的數(shù)據(jù)。
案例:
父組件
子組件觸發(fā)事件 并傳值 :
this.$emit("自定義事件,可以和原生事件同名:click",參數(shù)1,參數(shù)2,)
<Box @事件="fn"/>
<template> <div> <!--首先用v-for循環(huán)把值遍歷出來 ,同時通過屬性綁定將父組件的值傳入子組件 --> <!-- 綁定一個自定義事件 --> <Box @mychange="change" v-for="(el,index) in arr" :title="el.title" :price="el.price" :count="el.count" :index="index" :key="index" ></Box> <button>{{msg}}:{{total}}</button> </div> </template> <script> import Box from "@/components/myheader.vue" //引入子組件 export default { name: 'VueApp', data() { return { msg:"父組件的總價", arr:[] //必須提前給后端數(shù)據(jù)創(chuàng)建一個數(shù)據(jù)容器,不然在數(shù)據(jù)請求過來之前會出錯 }; }, components:{ Box }, async mounted() { let res= await this.$axios("/test") //頁面掛載的時候請求的后端數(shù)據(jù) this.arr=res.data //將后端數(shù)據(jù)保存起來 console.log(this.arr) }, methods: { change(n,index){ this.arr[index].count=n //將子組件的值賦值給父組件 console.log(this.arr[index].count,n,99999) this.$set(this.arr,index,this.arr[index])//刷新頁面 } }, computed:{ total(){ //父組件計算總價 return this.arr.reduce((n1,n2)=>{ return n1+n2.price*n2.count },0) } } }; </script> <style scoped> </style>
子組件
<template> <div> 菜名:{{title}}, 數(shù)量:{{count}} <button @click="add">+</button> , 價格:{{price}}元 單菜總價:{{total}}元 </div> </template> <script> export default { name: 'VueBox', props:['title','price','count','index'], // 使用props屬性接受父組件的值 data() { return { }; }, mounted() { }, computed:{ total(){ return this.count*this.price //計算單樣菜品的總價 } }, methods: { add(){ // this.count++ //點擊事件,讓菜品數(shù)量增加 let n=this.count+1 this.$emit("mychange",n,this.index)//當(dāng)該函數(shù)運行時,就會去觸發(fā)mychange這個自定義事件,后邊可以寫傳參即n、this.index console.log(n,this.index,666666) } }, }; </script> <style scoped> </style>
首先已知該案例后端請求的數(shù)據(jù)為一個數(shù)組
效果圖:
此時我們隨意點擊一個+按鈕,如點擊第二個
根據(jù)父組件總價也隨著子組件的值改變而改變,說明傳值成功。
系統(tǒng)自帶的直接可以使用,不需要自定義事件,直接傳值過來
子:如 this.$emit("update:msg",100) 父:<Box v-bind:msg.sync="n" />
案例
父組件
<template> <div> <!--首先用v-for循環(huán)把值遍歷出來 ,同時通過屬性綁定將父組件的值傳入子組件 --> <!-- 給a1綁定sync --> <Box @mychange="change" v-for="(el,index) in arr" :title="el.title" :price="el.price" :count="el.count" :index="index" :key="el.id" :a1.sync="el.count" ></Box> <button>{{msg}}:{{total}}</button> </div> </template> <script> import Box from "@/components/myheader.vue" //引入子組件 export default { name: 'VueApp', data() { return { msg:"父組件的總價", arr:[] //必須提前給后端數(shù)據(jù)創(chuàng)建一個數(shù)據(jù)容器,不然在數(shù)據(jù)請求過來之前會出錯 }; }, components:{ Box }, async mounted() { let res= await this.$axios("/test") //頁面掛載的時候請求的后端數(shù)據(jù) this.arr=res.data //將后端數(shù)據(jù)保存起來 console.log(this.arr) }, methods: { change(n,index){ this.arr[index].count=n //將子組件的值賦值給父組件 console.log(this.arr[index].count,n,99999) this.$set(this.arr,index,this.arr[index])//刷新頁面 } }, computed:{ total(){ //父組件計算總價 return this.arr.reduce((n1,n2)=>{ return n1+n2.price*n2.count },0) } } }; </script> <style scoped> </style>
子組件
<template> <div> 菜名:{{title}}, 數(shù)量:{{count}} <button @click="add">+</button> <button @click="decline">-</button>, 價格:{{price}}元 單菜總價:{{total}}元 </div> </template> <script> export default { name: 'VueBox', props:['title','price','count','index','a1'], // 使用props屬性接受父組件的值 data() { return { }; }, mounted() { }, computed:{ total(){ return this.count*this.price //計算單樣菜品的總價 } }, methods: { add(){ // this.count++ //點擊事件,讓菜品數(shù)量增加 let n=this.count+1 this.$emit("mychange",n,this.index)//當(dāng)該函數(shù)運行時,就會去觸發(fā)mychange這個自定義事件,后邊可以寫傳參即n、this.index console.log(n,this.index,666666) }, decline(){ let i=this.count-1 this.$emit("update:a1",i,11) } }, }; </script> <style scoped> </style>
效果圖
此時點擊-按鈕,會發(fā)現(xiàn)和+按鈕實現(xiàn)的效果一樣
如點擊第一個-按鈕
依然能將子組件的值傳給父組件
v-model
v-model同樣也可以傳值實現(xiàn)雙向數(shù)據(jù)綁定,子組件由props接收value即可
到此這篇關(guān)于Vue中子組件向父組件傳值$emit、.sync的文章就介紹到這了,更多相關(guān)Vue子組件向父組件傳值內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue如何使用cookie、localStorage和sessionStorage進(jìn)行儲存數(shù)據(jù)
這篇文章主要介紹了vue如何使用cookie、localStorage和sessionStorage進(jìn)行儲存數(shù)據(jù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08Vue.js監(jiān)聽select2的值改變進(jìn)行查詢方式
這篇文章主要介紹了Vue.js監(jiān)聽select2的值改變進(jìn)行查詢方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04Vue利用computed配合watch實現(xiàn)監(jiān)聽多個屬性的變化
這篇文章主要給大家介紹了在Vue中巧用computed配合watch實現(xiàn)監(jiān)聽多個屬性的變化的方法,文中有詳細(xì)的代碼示例供大家參考,具有一定的參考價值,需要的朋友可以參考下2023-10-10uniapp微信小程序WebApi_openid、phone接口獲取代碼詳解
本文主要記錄了微信小程序接口調(diào)用的過程,首先查看uniapp文檔和微信API文檔,獲取openid和phone,然后通過uniapp實現(xiàn)獲取openid和電話號碼,但遇到了合法域名屏蔽的問題,最后通過將微信訪問遷移到后臺解決,需要的朋友可以參考下2024-10-10