vue.js - 子组件中发出事件的问题
问题描述
我仍然不明白如何发出事件,它显示了我的组件一(子)。我知道我做错了什么,但不知道具体是什么。应该隐藏一个组件。然后点击后应该显示。有什么帮助吗?
父组件
<template lang="pug">
one-component(v-on:welcome="weAreHere")
</template>
export default {
name: "Playlist",
data () {
return { }
},
component: {
one-component
},
methods: {
weAreHere() {
console.log("here we are!")
}
}
单组分
<template>
.mainBox1
button.burgerMenu(v-on:click="$emit('welcome')")
export default {
name: "one-component",
data () {
return {
show: true,
}
},
解决方案
我看到了你的困惑。您正在尝试从父组件引用子组件的方法。您有一个子组件使用 emit 来联系您希望在子组件中调用该方法的父组件。您应该在子组件中调用子方法。你不需要发射任何东西。
$emit
当您希望父母做某事时使用。
例如:
家长
<template>
<ChildComponent v-on:signal="myMethod"/>
</template>
<script>
methods: {
myMethod() {
//do something in parent after emit from child
}
}
</script>
在儿童中:
someMethod () {
this.$emit('signal')
}
当父母收到“信号”时,他们会打电话给myMethod
.
推荐阅读
- mysql - com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:“ver_date”的默认值无效
- php - PDO 插入/更新重复键
- bash - 显示来自“> /dev/null”的消息?
- python - 使用 python 脚本构建 debian 包
- node.js - 异步出现错误是问题
- sql - 编辑特定列上的 DB2 数据
- latex - 在乳胶 .tex 文件中插入 .sty 文件的内容
- godot - 简单的 3d 场景性能缓慢
- asp.net-core - 有没有办法创建 HTML 扩展方法来创建缓存破坏静态文件路径?
- python - 在 python 中存储来自 API 请求的某些数据