首页 > 解决方案 > 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.

Card Handler method Here's my card component

标签: javascriptvue.js

解决方案


您可以做的一件事是使用总线或事件总线。

您需要做的第一件事是创建事件总线并将其导出到某个地方,以便其他模块和组件可以使用它。

从'vue'导入Vue;

导出 const EventBus = new Vue();

你需要做的就是导入 Vue 库并导出它的一个实例。(在这种情况下,我将其称为 EventBus。)您实际上得到的是一个与 DOM 或应用程序的其余部分完全分离的组件。它上面存在的只是它的实例方法,所以它非常轻量级。

假设您有一个非常兴奋的组件,它感觉需要通知您的整个应用程序在有人点击它时它被点击了多少次。以下是您将如何使用 EventBus.emit(channel: string, payload1: any, ...) 来实现它。


推荐阅读