vue.js - 无法在父组件上监听子组件发出的事件
问题描述
我有两个这样设置的组件
FirstComponent.vue
<template>
<second-component @click="doThis" />
</template>
SecondComponent.vue
<template>
<img @click="doThisSecond" />
<template/>
虽然doThisSecond
确实有效,doThis
但不会触发。
我也尝试过,但我无法this.$emit('componentClicked')
从doThisSecond()
FirstComponent.vue
我错过了什么吗?
解决方案
语法和使用方法
第一个组件.vue
<template>
<second-component @doThisSecond="doThis" />
</template>
第二组件.vue
<template>
<img @click="this.$emit("doThisSecond")" />
<template/>
您可以使用方法发送数据
<template>
<img @click="emitDoThis" />
<template/>
<script>
export default {
data() {
return {
person: {
name: 'Hello',
}
};
},
methods: {
emitDoThis() {
this.$emit('doThisSecond', this.person.name);
}
}
</script>
推荐阅读
- api - 在 Python 请求模块中,在 POST 方法中,响应没有按预期获得,但 POSTMAN 工具中的相同操作按预期工作
- javascript - 我直接从教程中复制的简单代码不起作用,Javascript Notepad++
- php - 无法让 GETTEXT 在 MAMP 服务器和 Windows 10 上使用 PHP
- node.js - POST net::ERR_EMPTY_RESPONSE axios
- visual-studio-code - VS Code:更改菜单栏颜色
- sql - 如何显示连续日期
- r - R将操作写入函数并迭代
- https - Bazel 远程缓存不适用于证书/TLS
- javascript - 从授权服务器收到无效响应。Xero 与 Bagisto 的集成
- python - 产生这个 {20702 20709 20695 20703} 的方程来产生这个 {20714}