4 回答

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

TA貢獻1824條經驗 獲得超5個贊
更新:
在 vue 3 腳本設置中你會做
const emits = defineEmits(["updatedcount"])
emits("updatedcount", store.count);

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

TA貢獻1802條經驗 獲得超4個贊
在 vue3 中你必須定義 emits,你的子組件看起來像這樣:
子組件.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>
添加回答
舉報