4 回答

TA貢獻(xiàn)1866條經(jīng)驗(yàn) 獲得超5個(gè)贊
我認(rèn)為您需要emits
在組件中定義:
export default {
? name: "HelloWorld",
? emits: ["updatedcount"], // <--- add this line
? setup(_,{ emit }) {
? ? ...
? },
};

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超5個(gè)贊
更新:
在 vue 3 腳本設(shè)置中你會(huì)做
const emits = defineEmits(["updatedcount"])
emits("updatedcount", store.count);

TA貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超3個(gè)贊
當(dāng)在我自己的 vue 3 應(yīng)用程序中看到此錯(cuò)誤時(shí),我發(fā)現(xiàn)將組件的模板內(nèi)容包裝在一個(gè)空的 div 中可以解決我認(rèn)為與錯(cuò)誤消息的“無(wú)法自動(dòng)繼承”部分有關(guān)的問(wèn)題。
似乎 vue 的工作方式是 vue 將嘗試對(duì) @click 和 @input 等常見事件使用屬性繼承以傳遞給底層元素,但是當(dāng)組件的根部有多個(gè)同級(jí)元素時(shí),它不知道要傳遞哪個(gè)選擇。
請(qǐng)注意,這些事件可能會(huì)改變某些行為,因?yàn)樾碌陌b父 div 現(xiàn)在將直接綁定到它的事件。
<template>
<div>
<h1>{{ store.count }}</h1>
<button @click="fired">click me</button>
</div>
</template>

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超4個(gè)贊
在 vue3 中你必須定義 emits,你的子組件看起來(lái)像這樣:
子組件.vue:
<template>
<h1>{{ store.count }}</h1>
<button @click="fired">click me</button>
</template>
<script>
import useStore from "../store/store.js";
export default {
name: "HelloWorld",
emits :{
updatedcount: null, <--- add this lines
},
data: () => ({
store: useStore(),
}),
methods:
fire () {
store.count++;
this.$emit("updatedcount", store.count);
},
};
</script>
添加回答
舉報(bào)