gpt4 book ai didi

javascript - vue 3发出警告 "Extraneous non-emits event listeners"

转载 作者:行者123 更新时间:2023-12-03 06:43:43 26 4
gpt4 key购买 nike

我正在尝试使用组合 API 从子级向父级发送数据
我收到以下警告。

[Vue warn]: Extraneous non-emits event listeners (updatedcount) were passed to component but could not be automatically inherited because component renders fragment or text root nodes. If the listener is intended to be a component custom event listener only, declare it using the "emits" option.at <HelloWorld onUpdatedcount=fn > at


子组件.vue

<template>
<h1>{{ store.count }}</h1>
<button @click="fired">click me</button>
</template>

<script>
import useStore from "../store/store.js";
export default {
name: "HelloWorld",
setup(_,{ emit }) {
const store = useStore();

const fired = () => {
store.count++;
emit("updatedcount", store.count);
};

return {
store,
fired
};
},
};
</script>


父组件.vue

<template>
<div>
{{ hello }}
<br />
<br />
<input type="text" v-model="hello.searchQuery" />
<br><br>
<button @click="hello.count--">click me too!</button>
<hello-world @updatedcount="mydata" />
</div>
</template>

<script>
import HelloWorld from "./components/HelloWorld.vue";
import useStore from "./store/store.js";

export default {
components: {
HelloWorld,
},
setup() {
const hello = useStore();

function mydata(event) {
console.log(event);
}

return {
hello,
mydata
};
},
};
</script>

最佳答案

我认为您需要定义 emits在您的组件中:https://v3.vuejs.org/guide/component-custom-events.html#defining-custom-events

export default {
name: "HelloWorld",
emits: ["updatedcount"], // <--- add this line
setup(_,{ emit }) {
...
},
};

关于javascript - vue 3发出警告 "Extraneous non-emits event listeners",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64220737/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com