vue3觸發(fā)父組件兩種寫法
vue3觸發(fā)父組件兩種寫法
1、正常寫法
子組件:
import { defineComponent } from 'vue';
export default defineComponent({
emits: ["testEmi"],
setup(props, context) {
const changeCollapse = () => {
//觸發(fā)父組件事件
context.emit("testEmi")
}
return {
testEmi
}
}
})父組件:
<test @testEmit="testEmi" />
2、 語法糖寫法
子組件:
const emit = defineEmits(["downloadTemp"]);
const downloadTemp = () => {
emit("downloadTemp", "12");
};父組件:
<UpDownload @downloadTemp="downloadTempSms"/>
在 <script setup> 中必須使用 defineProps 和 defineEmits API 來聲明 props 和 emits
補(bǔ)充:vue子組件調(diào)用父組件的3種方法,超實(shí)用
1. 直接在子組件中通過this.$parent.event來調(diào)用父組件的方法
父組件:
<template>
<div>
<child></child>
</div>
</template>
<script>
import child from '~/components/dam/child';
export default {
components: {
child
},
methods: {
fatherMethod() {
console.log('測試');
}
}
};
</script>子組件:
<template>
<div>
<button @click="childMethod()">點(diǎn)擊</button>
</div>
</template>
<script>
export default {
methods: {
childMethod() {
this.$parent.fatherMethod();
}
}
};2. 在子組件里用$emit向父組件觸發(fā)一個(gè)事件,父組件監(jiān)聽這個(gè)事件
父組件:
<template>
<div>
<child @fMethod="fatherMethod"></child>
</div>
</template>
<script>
import child from '~/components/dam/child';
export default {
components: {
child
},
methods: {
fatherMethod(data) {
console.log(data);
}
}
};
</script>子組件:
<template>
<div>
<button @click="childMethod()">點(diǎn)擊</button>
</div>
</template>
<script>
export default {
methods: {
childMethod() {
this.$emit('fMethod',data);
}
}
};
</script>3. 父組件把方法傳入子組件中,在子組件里直接調(diào)用
父組件:
<template>
<div>
<child :fatherMethod="fatherMethod"></child>
</div>
</template>
<script>
import child from '~/components/dam/child';
export default {
components: {
child
},
methods: {
fatherMethod() {
console.log('測試');
}
}
};
</script>子組件:
<template>
<div>
<button @click="childMethod()">點(diǎn)擊</button>
</div>
</template>
<script>
export default {
props: {
fatherMethod: {
type: Function,
default: null
}
},
methods: {
childMethod() {
if (this.fatherMethod) {
this.fatherMethod();
}
}
}
};
</script>到此這篇關(guān)于vue3觸發(fā)父組件兩種寫法的文章就介紹到這了,更多相關(guān)vue3觸發(fā)父組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue + echarts實(shí)現(xiàn)中國省份地圖點(diǎn)擊聯(lián)動
這篇文章主要介紹了vue + echarts實(shí)現(xiàn)中國地圖省份點(diǎn)擊聯(lián)動,需要的朋友可以參考下2022-04-04
vue.js提交按鈕時(shí)進(jìn)行簡單的if判斷表達(dá)式詳解
這篇文章主要給大家介紹了關(guān)于vue.js提交按鈕時(shí)如何進(jìn)行簡單的if判斷表達(dá)式的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08
Vue3使用Proxy實(shí)現(xiàn)數(shù)據(jù)監(jiān)聽的原因分析
在本篇文章里小編給大家整理的是一篇關(guān)于Vue3使用Proxy實(shí)現(xiàn)數(shù)據(jù)監(jiān)聽的原因分析內(nèi)容,有需要的朋友們可以跟著學(xué)習(xí)參考下。2021-11-11
vue2 拖動排序 vuedraggable組件的實(shí)現(xiàn)
這篇文章主要介紹了vue2 拖動排序 vuedraggable組件的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Vue?ElementUI在el-table中使用el-popover問題
這篇文章主要介紹了Vue?ElementUI在el-table中使用el-popover問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04

