超簡(jiǎn)單易懂的vue組件傳值
前言
vue中的組件傳值大家應(yīng)該都不陌生,今天用兩個(gè)簡(jiǎn)單易懂的小案例教大家在項(xiàng)目中如何使用父?jìng)髯?、子傳父組件之間的數(shù)據(jù)傳遞。
實(shí)現(xiàn)思路
- 父?jìng)髯樱?/strong> 在父組件中給子組件標(biāo)簽上綁定一個(gè)屬性, 屬性上掛載需要傳遞的值,在子組件通過(guò)
props:['自定義屬性名']
來(lái)接收數(shù)據(jù)。 - 子傳父: 在子組件中自定義一個(gè)事件,調(diào)用這個(gè)事件后,子組件通過(guò)
this.$emit('自定義事件名',要傳遞的數(shù)據(jù))
發(fā)送父組件可以監(jiān)聽(tīng)的數(shù)據(jù),最后父組件監(jiān)聽(tīng)子組件事件,調(diào)用事件并接收傳遞過(guò)來(lái)的數(shù)據(jù)。
話(huà)不多說(shuō),下面進(jìn)入實(shí)戰(zhàn)
實(shí)例1:父?jìng)髯?/h2>
本篇小實(shí)例主要是模擬父組件向不同子組件傳遞不同數(shù)據(jù)的情況。
父組件 index.vue
<template> <!-- 父組件 --> <div> <Child :message="informtion" v-if="typeCode == '0'"></Child> <Electronic :message="dataList" v-if="typeCode == '1'"></Electronic> </div> </template> <script> // 引入子組件 import Child from "./subassembly/seed.vue"; import Electronic from "./subassembly/sons.vue"; export default { data() { return { typeCode: "0",//通過(guò)"0" "1"判斷顯示哪個(gè)頁(yè)面;0:子組件1頁(yè)面;1:子組件2頁(yè)面 informtion:"我是傳遞給子組件1的數(shù)據(jù)",//要傳遞給子組件1的數(shù)據(jù) dataList:"我是傳遞給子組件2的數(shù)據(jù)",//要傳遞給子組件2的數(shù)據(jù) }; }, //一定要注冊(cè)組件 components: { Child, Electronic, }, }; </script>
子組件1 seed.vue
<template> <!-- 子組件1 --> <h2>我是子組件1<br />接收父組件值:{{ informtion }}</h2> </template> <script> export default { data() { return { informtion: "",//用于賦值 }; }, props: { // message用于接收 message: { type: String, //驗(yàn)證類(lèi)型,也可以驗(yàn)證其他類(lèi)型 default: "", //如果父組件傳值,則用父組件的值渲染,反之使用默認(rèn)值 }, }, mounted() { console.log(this.message); //父組件傳遞過(guò)來(lái)的數(shù)據(jù) // 賦值操作 let str = this.message; this.informtion = str; }, }; </script>
子組件2 sons.vue
<template> <!-- 子組件2 --> <h2>我是子組件2<br />接收父組件值:{{ dataList }}</h2> </template> <script> export default { data() { return { dataList: "",//用于賦值 }; }, props: { // message用于接收 message: { type: String, //驗(yàn)證類(lèi)型,也可以驗(yàn)證其他類(lèi)型 default: "", //如果父組件傳值,則用父組件的值渲染,反之使用默認(rèn)值 }, }, mounted() { console.log(this.message); //父組件傳遞過(guò)來(lái)的數(shù)據(jù) // 賦值操作 let str = this.message; this.dataList = str; }, }; </script>
實(shí)現(xiàn)效果
1. 當(dāng)
typeCode
為 “0” 時(shí),頁(yè)面內(nèi)容如下:
2. 當(dāng)
typeCode
為 “1” 時(shí),頁(yè)面內(nèi)容如下:
實(shí)例2:子傳父
本篇小實(shí)例主要是模擬不同子組件向父組件傳遞數(shù)據(jù)的情況。
seed.vue 子組件1
<template> <!-- 子組件1 --> <button @click="seedOnclick">我是子組件1</button> </template> <script> export default { data() { return { seedCode: "Romantic never die!", //子傳父要傳遞的值 }; }, methods: { seedOnclick() { this.$emit("seed", this.seedCode); //參數(shù)1:自定義事件;參數(shù)2:要傳遞的值 }, }, }; </script>
sons.vue 子組件2
<template> <!-- 子組件2 --> <button @click="sonsOnclick">我是子組件2</button> </template> <script> export default { data() { return { dataListCode: "world peace!", //子傳父要傳遞的值 }; }, methods: { sonsOnclick() { this.$emit("sons", this.dataListCode); //參數(shù)1:自定義事件;參數(shù)2:要傳遞的值 }, }, }; </script>
index.vue 父組件
<template> <!-- 父組件 --> <div> <Child @seed="seedAccept" v-if="typeCode == '0'"></Child> <Electronic @sons="sonsAccept" v-if="typeCode == '1'"></Electronic> </div> </template> <script> // 引入子組件 import Child from "./subassembly/seed.vue"; import Electronic from "./subassembly/sons.vue"; export default { data() { return { typeCode: "0", //通過(guò)"0" "1"判斷顯示哪個(gè)頁(yè)面;0:子組件1頁(yè)面;1:子組件2頁(yè)面 }; }, //一定要注冊(cè)組件 components: { Child, Electronic, }, methods: { seedAccept(data) { console.log(data, "子組件1傳給父組件的值"); }, sonsAccept(data) { console.log(data, "子組件2傳給父組件的值"); }, }, }; </script>
實(shí)現(xiàn)效果
1. 當(dāng)
typeCode
為 “0” 時(shí),頁(yè)面內(nèi)容如下:
2. 當(dāng)
typeCode
為 “1” 時(shí),頁(yè)面內(nèi)容如下:
總結(jié)
本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
vue實(shí)現(xiàn)消息向上無(wú)縫滾動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)消息向上無(wú)縫滾動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04v-if 導(dǎo)致 elementui 表單校驗(yàn)失效問(wèn)題解決方案
在使用 elementui 表單的過(guò)程中,某些表單項(xiàng)需要通過(guò) v-if 來(lái)判斷是否展示,但是這些表單項(xiàng)出現(xiàn)了檢驗(yàn)失效的問(wèn)題,今天小編給大家介紹v-if 導(dǎo)致 elementui 表單校驗(yàn)失效問(wèn)題解決方案,感興趣的朋友一起看看吧2024-01-01Vue3.2單文件組件setup的語(yǔ)法糖與新特性總結(jié)
ue3上線(xiàn)已經(jīng)很久了,許多小伙伴應(yīng)該都已經(jīng)使用過(guò)vue3了,下面這篇文章主要給大家介紹了關(guān)于Vue3.2單文件組件setup的語(yǔ)法糖與新特性總結(jié)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07vue實(shí)現(xiàn)動(dòng)態(tài)路由添加功能的簡(jiǎn)單方法(無(wú)廢話(huà)版本)
ue動(dòng)態(tài)路由(約定路由),聽(tīng)起來(lái)好像很玄乎的樣子,但是你要是理解了實(shí)現(xiàn)思路,你會(huì)發(fā)現(xiàn)沒(méi)有想象中的那么難,下面這篇文章主要給大家介紹了關(guān)于vue實(shí)現(xiàn)動(dòng)態(tài)路由添加功能的簡(jiǎn)單方法,需要的朋友可以參考下2023-02-02vue實(shí)現(xiàn)簡(jiǎn)單的購(gòu)物車(chē)小案例
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)簡(jiǎn)單的購(gòu)物車(chē)小案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07在Vue3項(xiàng)目中使用如何echarts問(wèn)題
這篇文章主要介紹了在Vue3項(xiàng)目中使用如何echarts問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05詳解vue 數(shù)組和對(duì)象渲染問(wèn)題
這篇文章主要介紹了詳解vue 數(shù)組和對(duì)象渲染問(wèn)題,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09