Vue3子組件向父組件傳值的兩種實(shí)現(xiàn)方式
有兩種方式可以實(shí)現(xiàn)。
方式一:
父組件傳送一個(gè)處理方法給子組件,子組件調(diào)用這個(gè)處理方法把父組件關(guān)心的值作為參數(shù)傳給這個(gè)處理方法。
例子:================子組件 (父組件在下面)
<template>
<el-form :inline="true" :model="request" class="demo-form-inline">
<el-form-item>
<target-type-drop-down />
</el-form-item>
<el-form-item>
<include-type />
</el-form-item>
<el-form-item label="Include">
<el-input v-model="request.number" placeholder="3" />
</el-form-item>
<el-form-item>
# 當(dāng)button被按下時(shí),子組件里的request變量內(nèi)容就會(huì)傳給父組件的處理方法
<el-button type="primary" @click="parentMethod(request)"
>Generate Req</el-button
>
</el-form-item>
</el-form>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
import { subRequest } from '@cp/MyTypes'
const request = ref<subRequest>({number: 3});
# 定義從父組件接收的處理方法
defineProps({
parentMethod: {
type: Function,
default: () => {},
},
})
================父組件
<template>
# 父組件傳送給子組件的處理方法
<request-gen v-if="showGenFlag" :parentMethod="childValueHandlingMethod" />
</template>
<script lang="ts" setup>
import { ref } from 'vue'
import { subRequest } from '@cp/MyTypes'
const subRequests = ref<subRequest[]>([])
const childValueHandlingMethod = (request: subRequest) => {
console.log('Hello I am from children component', request)
# 父組件把子組件傳過來的值放在了自己內(nèi)部變量數(shù)組里
subRequests.value.push(request)
}
</script>方式二:子組件發(fā)送emit方法給父組件
例子在子組件里,就兩步要做:1. 定義emits事件,2,在想要的時(shí)機(jī)發(fā)送emits事件
================子組件 (父組件在下面)
<template>
<el-dropdown class="margin-right">
<el-button type="info">
{{ selectedValue
}}<el-icon class="el-icon--right"><arrow-down /></el-icon>
</el-button>
<template #dropdown>
<el-dropdown-menu>
<el-dropdown-item
v-for="theType in items"
:key="`${theType}`"
@click="click(theType)"
>{{ theType }}</el-dropdown-item
>
</el-dropdown-menu>
</template>
</el-dropdown>
</template>
<script lang="ts" setup>
import { computed, defineEmits, ref } from 'vue'
import { ToRef } from 'vue-demi'
interface Props {
items: String[]
title: String
}
const props = defineProps<Props>()
# 定義要發(fā)送的emit事件
const emit = defineEmits(['dropDownValueChange'])
const selectedValue = ref(props.title)
const click = (theType: string) => {
console.log('clicked', theType)
selectedValue.value = theType
# 發(fā)送事件,theType就是發(fā)送出去的值
emit('dropDownValueChange', theType)
}
</script>
================父組件
<template>
<common-drop-down
title="Fruits"
:items="items"
@dropDownValueChange="fruitValueChange"
/>
</template>
<script lang="ts" setup>
import { ToRef } from "vue-demi"
const items = ['Apple', 'Orange', 'Pineapple', 'Banana']
const fruitValueChange = (e: any): void => {
console.log('in parent compoennt, e=', e)
}
</script>
這兩種方法我都在自己代碼里用過了,親測可用。優(yōu)不優(yōu)雅就另說了,先能用再說吧
總結(jié)
到此這篇關(guān)于Vue3子組件向父組件傳值的兩種實(shí)現(xiàn)方式的文章就介紹到這了,更多相關(guān)Vue3子組件向父組件傳值內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue 父組件獲取子組件里面的data數(shù)據(jù)(實(shí)現(xiàn)步驟)
- Vue子組件如何修改父組件數(shù)據(jù)的方法及注意事項(xiàng)
- vue3父子組件通信之子組件修改父組件傳過來的值
- vue父組件值變化但子組件不刷新的三種解決方案
- Vue子組件調(diào)用父組件事件的3種方法實(shí)例
- vue中父組件通過props向子組件傳遞數(shù)據(jù)但子組件接收不到解決辦法
- vue子組件實(shí)時(shí)獲取父組件的數(shù)據(jù)實(shí)現(xiàn)
- 關(guān)于VUE點(diǎn)擊父組件按鈕跳轉(zhuǎn)到子組件的問題及解決方案
相關(guān)文章
Element?UI中v-infinite-scroll無限滾動(dòng)組件使用詳解
在移動(dòng)端數(shù)據(jù)的更新中許多方法孕育而生,無限滾輪也是解決的方案一種,Element-ui為vue開發(fā)了一個(gè)事件(v-infinite-scroll),下面這篇文章主要給大家介紹了關(guān)于Element?UI中v-infinite-scroll無限滾動(dòng)組件使用的相關(guān)資料,需要的朋友可以參考下2023-02-02
vue3中update:modelValue的使用與不生效問題解決
現(xiàn)在vue3的使用越來越普遍了,vue3這方面的學(xué)習(xí)我們要趕上,下面這篇文章主要給大家介紹了關(guān)于vue3中update:modelValue的使用與不生效問題的解決方法,需要的朋友可以參考下2022-03-03
vue3 find 數(shù)組查找的幾種實(shí)現(xiàn)方式
在Vue3中,如果你想使用find方法來查找數(shù)組中的元素,你可以直接在模板中使用該方法,或者在計(jì)算屬性或方法中實(shí)現(xiàn),下面就來介紹幾種不同的使用方式,感興趣的可以了解一下2025-11-11
詳解vue-router數(shù)據(jù)加載與緩存使用總結(jié)
這篇文章主要介紹了詳解vue-router數(shù)據(jù)加載與緩存使用總結(jié),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-10-10
詳解vue-cli本地環(huán)境API代理設(shè)置和解決跨域
這篇文章主要介紹了詳解vue-cli本地環(huán)境API代理設(shè)置和解決跨域,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09

