vue3的父傳子問題(單項數(shù)據(jù)流)
vue3父傳子(單項數(shù)據(jù)流)
父組件通過屬性綁定傳遞數(shù)據(jù)
子組件內(nèi)部通過props接收數(shù)據(jù)
基本數(shù)據(jù)類型---改變父組件的數(shù)據(jù)--只改變了 父組件的的數(shù)據(jù)在子組件的顯示
通過動態(tài)屬性綁定 傳遞list給子組件
在子組件內(nèi)部修改父組件的復(fù)雜數(shù)據(jù)類型的數(shù)據(jù)---可以修改
vue3 父子傳參 簡單易懂
父傳子
1、 在父組件的子組件標(biāo)簽上通過 :傳遞到子組件的數(shù)據(jù)名="需要傳遞的數(shù)據(jù)"
在這里為了大家區(qū)分我將父組件中的數(shù)據(jù)定義為 : fatherData ,
子組件需要接收的數(shù)據(jù)定義為: sonData 。
// 父組件 <template> ? <div class="about"> ? ? {{fatherData}} ? ? <!-- 父傳子 --> ? ? <!-- 1、 在父組件的子組件標(biāo)簽上通過 :傳遞到子組件的數(shù)據(jù)名="需要傳遞的數(shù)據(jù)" --> ? ? <children :sonData="fatherData"></children> ? </div> </template> ? <script> import children from "@/components/children" import { defineComponent,reactive,toRefs } from "vue"; export default defineComponent({ ? components:{ ? ? children, ? }, name:"about", setup(){ ? const state = reactive({ ? ? fatherData:"hello" ? }) ? return { ? ? ...toRefs(state) ? } } ? }) </script>
2、子組件依舊通過 props 來接收并且在模版中使用
那么大多數(shù)情況下都會去通過父組件傳遞到子組件中的數(shù)據(jù),根據(jù)這個數(shù)據(jù)去做一些特定的功能或者請求數(shù)據(jù)等等。
在 setup 鉤子中第一個參數(shù) props 就可以訪問到父組件傳遞的數(shù)據(jù),那么在函數(shù)中也是通過: props.父組件傳遞數(shù)據(jù)的名稱 來操作該數(shù)據(jù)。
setup函數(shù)接收props作為其第一個參數(shù),props對象是響應(yīng)式的(單向的父—>子),watchEffect或watch會觀察和響應(yīng)props的更新。不要對props對象進(jìn)行解構(gòu),那樣會失去響應(yīng)性。在開發(fā)過程中,props對象對用戶空間代碼時不可變的,用戶嘗試修改props時會觸發(fā)警告。
// 子組件 <template> ? <div class="children"> ? ? <!-- 3、在子組件模版中使用 --> ? ? {{sonData}} ? </div> </template> <script> export default { name:"Children", // 2、子組件通過 props 來接收 ? props:["sonData"], ? setup(props){ ? ? function childrenbut(){ ? ? ? // props.sonData ?可以訪問到父組件傳遞的數(shù)據(jù) ? ? ? console.log(props.sonData); ? ? } ? ? return { ? ? ? childrenbut ? ? } ? } } </script>
子傳父
1、子組件觸發(fā)事件通過 setup 的第二個參數(shù) context.emit 來實現(xiàn)子傳父
context 上下文對象。
// 子組件 <template> ? <div class="children"> ? ? {{sonData}} ? ? <!-- 1、觸發(fā)事件 --> ? ? <button @click="childrenbut">子組件按鈕</button> ? </div> </template> <script> export default { name:"Children", ? setup(props,context){ ? ? function childrenbut(){ ? ? ? console.log(context); ? ? ? // 2、通過context.emit 實現(xiàn)子傳父 ? ? ? // 第一個參數(shù)為 方法名,第二個參數(shù)為 需要傳遞的數(shù)據(jù) ? ? ? context.emit("change",'world') ? ? } ? ? return { ? ? ? childrenbut, ? ? } ? } } </script>
context 也可以使用結(jié)構(gòu)的形式這樣寫
// 子組件 <script> export default { name:"Children", ? // 通過結(jié)構(gòu) 之后直接寫 emit {emit} ? setup(props,{emit}){ ? ? function childrenbut(){ ? ? ? // 省去 context.emit ? ? ? emit("change",'world') ? ? } ? ? return { ? ? ? childrenbut, ? ? } ? } } </script>
最后總結(jié)一下:
在 vue3 中無論是父傳子還是子傳父其實與 vue2 中都相差無幾,
思想大多一樣,不一樣的是 vue3 需要通過調(diào)用各種各樣的鉤子來實現(xiàn)傳參。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決echarts圖表使用v-show控制圖表顯示不全的問題
這篇文章主要介紹了解決echarts圖表使用v-show控制圖表顯示不全的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07Vue2.0實現(xiàn)組件之間數(shù)據(jù)交互和通信操作示例
這篇文章主要介紹了Vue2.0實現(xiàn)組件之間數(shù)據(jù)交互和通信操作,結(jié)合實例形式分析了vue2.0組件之間通信的原理、實現(xiàn)方法及相關(guān)操作注意事項,需要的朋友可以參考下2019-05-05詳解vue.js移動端導(dǎo)航navigationbar的封裝
本篇文章主要介紹了vue.js移動端導(dǎo)航navigationbar的封裝,具有一定的參考價值,有興趣的可以了解一下2017-07-07詳解如何使用vue和electron開發(fā)一個桌面應(yīng)用
這篇文章主要為大家介紹了詳解如何使用vue和electron開發(fā)一個桌面應(yīng)用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03