vue3+vite+ts父子組件之間的傳值
前言
提示:這里僅描述<script lang="ts" setup>中的寫法,另外一種寫法的話,基本類似,這里不做展示了
隨著vue2的落幕,vue3越來成熟,有必要更新一下vue3的父子組件之間的傳值方式,和vue2的大差不差,都是一個道理,只不過寫法上出現(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,只不過要定義一下這個變量
const systemDialogref = ref()
// 點擊doShow這個方法,使用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可以傳遞多個任意類型的值,類似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都是傳給子組件的值,由于子組件是一個彈框,要用到ref的方式打開子組件的彈框
二、子組件向父組件傳值
代碼如下(子組件示例):
<script lang="ts" setup>
import { ref } from "vue";
import { ElMessageBox } from "element-plus";
// defineEmits中可以寫多個方法
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", "這是子組件傳遞的一個值");
};
</script>
<template>
<div>
<el-dialog v-model="dialogVisible" title="Tips" width="30%" :before-close="handleClose">
<el-button type="danger" plain @click="change">寫一個按鈕觸發(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,只不過要定義一下這個變量
const systemDialogref = ref()
// 點擊doShow這個方法,使用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寫法不一樣
三、非父子組件傳值,也就是任意兩個組件的傳值,和vue2基本相似,這里就不描述了,vue3里面建議大家使用pinia來進行傳值。
到此這篇關(guān)于vue3+vite+ts父子組件之間的傳值的文章就介紹到這了,更多相關(guān)vue3+vite+ts父子傳值內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用vue + less 實現(xiàn)簡單換膚功能的示例
下面小編就為大家分享一篇使用vue + less 實現(xiàn)簡單換膚功能的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02
Vue-cli 如何將px轉(zhuǎn)化為rem適配移動端
這篇文章主要介紹了Vue-cli 如何將px轉(zhuǎn)化為rem適配移動端,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-07-07
解決element-ui table設(shè)置列fixed時X軸滾動條無法拖動問題
這篇文章主要介紹了解決element-ui table設(shè)置列fixed時X軸滾動條無法拖動問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10

