我编写了一个组件my-component
,它通过this.$emit('my-message')
发出消息my-message
。
为了对这个消息采取行动,我尝试使用
<my-component @my-message="()=>console.log('hello')" />
当看控制台时,我看到警告(然后是错误(
Property or method "console" is not defined on the instance but referenced during render
由于犯了很多次错误,我认为错误来自使用胖箭头的上下文(之前的this
与that = this
(,但在这里我迷失了方向。
console
暴露为Window.console
,但如果我使用完整形式,Window
会生成相同的错误
这在Vue中不起作用。
传递方法名称而不是
<template>
<button @click="log('test')">
test
</button>
</template>
<script>
import { defineComponent } from "vue";
export default defineComponent({
methods: {
log(value) {
console.log(value)
}
}
});
</script>