vue3+vite+ts父子組件之間的傳值
前言
提示:這里僅描述<script lang="ts" setup>中的寫法,另外一種寫法的話,基本類似,這里不做展示了
隨著vue2的落幕,vue3越來成熟,有必要更新一下vue3的父子組件之間的傳值方式,和vue2的大差不差,都是一個(gè)道理,只不過寫法上出現(xiàn)了差異,vue3+vite+ts安裝這里就不寫了,由于文章中使用了element-plus的dialog組件作為子組件的內(nèi)容,所以前提工作是先安裝一下element-plus
一、父組件向子組件傳值
代碼如下(父組件示例):
<script setup lang="ts"> import systemDialog from '../../components/systemDialog.vue' import { reactive, ref } from "vue"; const perInfo = reactive([ { name: '張三', age: 20 }, { name: '李四', age: 25 }, ]) // vue2中的ref,vue3中也一樣是使用ref,只不過要定義一下這個(gè)變量 const systemDialogref = ref() // 點(diǎn)擊doShow這個(gè)方法,使用ref的方式將子組件的彈框調(diào)用起來,并且傳值過去, const doShow = () => { let str = '這是ref傳過去的值' systemDialogref.value.showDialog(str) } </script> <template> <div> <el-button text @click="doShow">click to open the Dialog</el-button> <systemDialog ref="systemDialogref" :perInfo="perInfo" msg="這是一段文本" /> </div> </template> <style scoped> </style>
代碼如下(子組件示例):
<script lang="ts" setup> import { ref } from "vue"; import { ElMessageBox } from "element-plus"; // defineProps可以傳遞多個(gè)任意類型的值,類似vue2中的props defineProps<{ perInfo: Array<any>; msg: string }>(); const dialogVisible = ref(false); const handleClose = (done: () => void) => { ElMessageBox.confirm("Are you sure to close this dialog?") .then(() => { done(); }) .catch(() => { // catch error }); }; const testData = ref(); const showDialog = (val: any) => { testData.value = val; dialogVisible.value = true; }; // 父組件使用ref調(diào)用showDialog方法,用到defineExpose將showDialog拋出去,直接用,不需要引入 defineExpose({ showDialog }); </script> <template> <div> <el-dialog v-model="dialogVisible" title="Tips" width="30%" :before-close="handleClose"> <div>下面的v-for循環(huán)就是父組件傳過來的值</div> <div v-for="(item, index) in perInfo" :key="index"> 名字: {{ item.name }} 年齡: {{ item.age }} </div> <div>下面是通過ref父組件傳給子組件的值</div> <div>{{ testData }}</div> <template #footer> <span class="dialog-footer"> <el-button @click="dialogVisible = false">Cancel</el-button> <el-button type="primary" @click="dialogVisible = false">Confirm</el-button> </span> </template> </el-dialog> </div> </template> <style scoped> .dialog-footer button:first-child { margin-right: 10px; } </style>
注意:父組件代碼中的perInfo和msg都是傳給子組件的值,由于子組件是一個(gè)彈框,要用到ref的方式打開子組件的彈框
二、子組件向父組件傳值
代碼如下(子組件示例):
<script lang="ts" setup> import { ref } from "vue"; import { ElMessageBox } from "element-plus"; // defineEmits中可以寫多個(gè)方法 const emit = defineEmits(["send-data"]); const dialogVisible = ref(false); const handleClose = (done: () => void) => { ElMessageBox.confirm("Are you sure to close this dialog?") .then(() => { done(); }) .catch(() => { // catch error }); }; const showDialog = () => { dialogVisible.value = true; }; // 父組件使用ref調(diào)用showDialog方法,用到defineExpose將showDialog拋出去,直接用,不需要引入 defineExpose({ showDialog }); // 觸發(fā)事件將子組件的值傳遞給父組件,send-data要在父組件中接收 const change = () => { emit("send-data", "這是子組件傳遞的一個(gè)值"); }; </script> <template> <div> <el-dialog v-model="dialogVisible" title="Tips" width="30%" :before-close="handleClose"> <el-button type="danger" plain @click="change">寫一個(gè)按鈕觸發(fā)將值傳給父組件</el-button> <template #footer> <span class="dialog-footer"> <el-button @click="dialogVisible = false">Cancel</el-button> <el-button type="primary" @click="dialogVisible = false">Confirm</el-button> </span> </template> </el-dialog> </div> </template> <style scoped> .dialog-footer button:first-child { margin-right: 10px; } </style>
代碼如下(父組件示例):
<script setup lang="ts"> import systemDialog from '../../components/systemDialog.vue' import { ref } from "vue"; // vue2中的ref,vue3中也一樣是使用ref,只不過要定義一下這個(gè)變量 const systemDialogref = ref() // 點(diǎn)擊doShow這個(gè)方法,使用ref的方式將子組件的彈框調(diào)用起來 const doShow = () => { systemDialogref.value.showDialog() } const sendData = ref(null) const handleReceivedData = (res: any) => { console.log(res); sendData.value = res } </script> <template> <div> <el-button text @click="doShow">click to open the Dialog</el-button> {{ sendData }} <systemDialog ref="systemDialogref" @send-data="handleReceivedData" /> </div> </template> <style scoped> </style>
子組件向父組件傳值,和vue2的很相似,邏輯也一樣,也是用到emit,只不過emit寫法不一樣
三、非父子組件傳值,也就是任意兩個(gè)組件的傳值,和vue2基本相似,這里就不描述了,vue3里面建議大家使用pinia來進(jìn)行傳值。
到此這篇關(guān)于vue3+vite+ts父子組件之間的傳值的文章就介紹到這了,更多相關(guān)vue3+vite+ts父子傳值內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue3+vite 動(dòng)態(tài)引用靜態(tài)資源及動(dòng)態(tài)引入assets文件夾圖片的多種方式
- vue3+vite assets動(dòng)態(tài)引入圖片的三種方法及解決打包后圖片路徑錯(cuò)誤不顯示的問題
- vue3+vite+TS中使用element-plus按需引入ElLoading、ElMessage樣式失效解決
- vue3+vite+ts使用monaco-editor編輯器的簡單步驟
- Vue3+Ts實(shí)現(xiàn)父子組件間傳值的兩種方式
- vue3?父子組件間相互傳值方式
- vue3父子組件傳值中props使用細(xì)節(jié)淺析
- vue3 父子組件傳值詳解
相關(guān)文章
使用vue + less 實(shí)現(xiàn)簡單換膚功能的示例
下面小編就為大家分享一篇使用vue + less 實(shí)現(xiàn)簡單換膚功能的示例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02Vue-cli 如何將px轉(zhuǎn)化為rem適配移動(dòng)端
這篇文章主要介紹了Vue-cli 如何將px轉(zhuǎn)化為rem適配移動(dòng)端,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-07-07vue3輸入單號和張數(shù)如何自動(dòng)生成連號的單號
最近遇到這樣的需求輸入連號事件,需要在表格中輸入物流單號,物流號碼,生成的數(shù)量,名稱,點(diǎn)擊確定自動(dòng)生成固定數(shù)量的連號物流單號,本文重點(diǎn)介紹vue3輸入單號和張數(shù),自動(dòng)生成連號的單號,感興趣的朋友一起看看吧2024-02-02vue引用外部JS并調(diào)用JS文件中的方法實(shí)例
我們在做vue項(xiàng)目時(shí),經(jīng)常會(huì)需要引入js,下面這篇文章主要給大家介紹了關(guān)于vue引用外部JS并調(diào)用JS文件中的方法的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02Vue3中的動(dòng)畫過渡實(shí)現(xiàn)技巧分享
在現(xiàn)代的前端開發(fā)中,用戶體驗(yàn)的重要性不言而喻,為了讓應(yīng)用程序更加生動(dòng)和引人注目,動(dòng)畫和過渡效果是必不可少的元素,本文將以 Vue3 為基礎(chǔ),深入探討如何在應(yīng)用程序中實(shí)現(xiàn)動(dòng)畫過渡,以及一些技巧和最佳實(shí)踐,需要的朋友可以參考下2025-01-01解決element-ui table設(shè)置列fixed時(shí)X軸滾動(dòng)條無法拖動(dòng)問題
這篇文章主要介紹了解決element-ui table設(shè)置列fixed時(shí)X軸滾動(dòng)條無法拖動(dòng)問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10