vue runtime-core.esm-bundler.js:6800 Uncaught TypeError: Cannot read properties of undefined (reading '$emit')

· 会写bug的大佬 · 229阅读 · 2022-09-29

详细描述

Vue3子组件给父组件传值报错 undefined (reading '$emit')

版本信息

Vue3

复现过程

代码如下:

const changeHide=()=>{
this.$emit("collapse", true);
}

这是在未使用语法糖的情况,调用changeHide时报错:

 

解决方案

1. 不使用语法糖解决方案

setup(props,context) {
const changeHide=()=>{
context.emit("collapse", true);
}
}

调用content.emit传递参数即可。

 

2. 使用语法糖

const emit = defineEmits(['collapse'])
emit('collapse', true)

接收参数:

props:{
collapse:{
type: Boolean,
default: false
}
},

文章有用

已有 1人 推荐该文章,推荐越多越容易获得的官方扶持

微信扫码分享