父组件 ——> 子组件 :props setup(props,ctx)
子组件 ——> 父组件 :emit setup(props,{emit}){ emit('sendMsg',state.result) } 或 setup(props,ctx) { ctx.emit('sendMsg',state.result) }
在vue3中使用 emit可能会报错警告:
Extraneous non-emits event listeners (sendMsg) 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.
export default {
name: "HelloWorld",
emits: ["sendMsg"],//添加这一行解决
props: {
msg: String,
},
setup(props, ctx) {
console.log("setup---props", props);
let state = reactive({
num1: 0,
num2: 0,
result: computed(() => {
return parseInt(state.num1) + parseInt(state.num2);
}),
});
let send = () => {
console.log("send", state.result);
ctx.emit("sendMsg", state.result);
};
return { state, send };
},
};