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