javascript - How can I pass data from component and use the data in a method?
问题描述
I'm working on a memory game and I'm trying to make the cards flip back automatically if they are not a pair. Here's the code:
I want to get the "isFlipped" data to be passed over to my main.js.
解决方案
您可以做的一件事是使用总线或事件总线。
您需要做的第一件事是创建事件总线并将其导出到某个地方,以便其他模块和组件可以使用它。
从'vue'导入Vue;
导出 const EventBus = new Vue();
你需要做的就是导入 Vue 库并导出它的一个实例。(在这种情况下,我将其称为 EventBus。)您实际上得到的是一个与 DOM 或应用程序的其余部分完全分离的组件。它上面存在的只是它的实例方法,所以它非常轻量级。
假设您有一个非常兴奋的组件,它感觉需要通知您的整个应用程序在有人点击它时它被点击了多少次。以下是您将如何使用 EventBus.emit(channel: string, payload1: any, ...) 来实现它。
推荐阅读
- sqlite - 无法从 UIWebView 和 WKWebview 访问相同的 SQLite 数据库
- python - 如何将打印件发送到以特定名称保存的 Word 文档?
- openapi - 如何在 Swagger2Markup API 要求中包含 JSON 正文 API 示例(从 Swagger YAML 转换时)?
- vb.net - 如何为我的用户控件上的属性创建一个列表以从中进行选择?
- regex - 微调正则表达式以在分隔符之间提取数据
- python - 如何将 JSON 数据发送到瓶子服务器并将值存储在 sqlite3 中?
- swift - 协议不符合从通用父类继承的类?
- python-3.x - 在 Lambda 中接收到模块的属性错误,但在本地机器上没有
- sql - 使用 LAG/LEAD 查找数据中的峰值会给出十年变化(即 9、99、999)以及正确的峰值
- peoplesoft - 根据 PO 获取申请 ID