欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue3+vite+ts父子組件之間的傳值

 更新時(shí)間:2023年12月25日 14:35:37   作者:demo11111111  
隨著vue2的落幕,vue3越來成熟,有必要更新一下vue3的父子組件之間的傳值方式,這里介紹下vue3+vite+ts父子組件之間的傳值方式實(shí)例詳解,感興趣的朋友一起看看吧

前言

提示:這里僅描述<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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 使用vue + less 實(shí)現(xiàn)簡單換膚功能的示例

    使用vue + less 實(shí)現(xiàn)簡單換膚功能的示例

    下面小編就為大家分享一篇使用vue + less 實(shí)現(xiàn)簡單換膚功能的示例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-02-02
  • Vue-cli 如何將px轉(zhuǎn)化為rem適配移動(dòng)端

    Vue-cli 如何將px轉(zhuǎn)化為rem適配移動(dòng)端

    這篇文章主要介紹了Vue-cli 如何將px轉(zhuǎn)化為rem適配移動(dòng)端,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2024-07-07
  • vue3輸入單號和張數(shù)如何自動(dòng)生成連號的單號

    vue3輸入單號和張數(shù)如何自動(dòng)生成連號的單號

    最近遇到這樣的需求輸入連號事件,需要在表格中輸入物流單號,物流號碼,生成的數(shù)量,名稱,點(diǎn)擊確定自動(dòng)生成固定數(shù)量的連號物流單號,本文重點(diǎn)介紹vue3輸入單號和張數(shù),自動(dòng)生成連號的單號,感興趣的朋友一起看看吧
    2024-02-02
  • vue選擇下拉框動(dòng)態(tài)變化表單方式

    vue選擇下拉框動(dòng)態(tài)變化表單方式

    這篇文章主要介紹了vue選擇下拉框動(dòng)態(tài)變化表單方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • vue之keepAlive使用案例詳解

    vue之keepAlive使用案例詳解

    這篇文章主要介紹了vue之keepAlive使用案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • 如何給element添加一個(gè)抽屜組件的方法步驟

    如何給element添加一個(gè)抽屜組件的方法步驟

    這篇文章主要介紹了如何給element添加一個(gè)抽屜組件的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • vue引用外部JS并調(diào)用JS文件中的方法實(shí)例

    vue引用外部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-02
  • 在vue項(xiàng)目中如何獲取視頻的時(shí)長

    在vue項(xiàng)目中如何獲取視頻的時(shí)長

    這篇文章主要介紹了在vue項(xiàng)目中如何獲取視頻的時(shí)長,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • Vue3中的動(dòng)畫過渡實(shí)現(xiàn)技巧分享

    Vue3中的動(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)問題

    這篇文章主要介紹了解決element-ui table設(shè)置列fixed時(shí)X軸滾動(dòng)條無法拖動(dòng)問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-10-10

最新評論