vue.js - 如何向 vue.js 中不是父级的另一个组件发出事件?
问题描述
因此,下面示例中的第一个组件只是简单地导入其他组件,然后在模板中呈现它们。在要导入的组件中,我有一个 Stages 组件和一个 StageExecutionTimes 组件。我想要做的是当用户单击 Stage 组件中的图标时,我想隐藏 StageExecutionTimes 组件。我的理解是,我需要使用$emit
Stages 组件将事件发送到父级,然后父级将隐藏 StageExecutionsComponent。我正在尝试设置showgraph
为布尔值,然后将其用作组件之间的道具。然后我试图改变showgraph
via的值$emit
。
下面的代码似乎不会影响或改变showgraph
主组件中的值
导入和渲染子组件的主组件
<template>
<div id="vue-main">
<NavBar></NavBar>
<transition name="fade">
<div :v-bind="showgraph"><StageExecutionTimes></StageExecutionTimes></div>
</transition>
<transition name="fade">
<Stages></Stages>
</transition>
<transition name="fade">
<Overview></Overview>
</transition>
<Footer></Footer>
</div>
</template>
<script>
import NavBar from "../components/NavBar.vue";
import Stages from "../components/execution_details/Stages.vue";
import Binaries from "../components/execution_details/Binaries.vue";
import StageExecutionTimes from "../components/graphs/StageExecutionTimes.vue";
import Overview from "../components/execution_details/Overview.vue";
export default {
name: "execution_details",
data() {
return {
loading: true,
showgraph: true
};
},
components: {
NavBar,
Stages,
StageExecutionTimes,
Overview,
},
../components/execution_details/Stages.vue";
<template>
<div class="stages">
<template v-if="job_execs.length > 0">
<h3>Stages</h3>
<a href="#" @click="!showgraph"><img src="@/assets/charticon.png"></a>
<transition name="fade" appear mode="out-in">
<table>
<tbody>
<template v-for="item in job_execs">
<tr>
<td
v-for="stage_execution in item.stage_execution"
:class="stage_execution.status.name"
:title="stage_execution.exec_node.name"
:key="stage_execution.stage.name"
>
<br />
{{ stage_execution.duration | durationReadable }}
<br />
{{ stage_execution.status.name }}
</td>
</tr>
</template>
</tbody>
</table>
</transition>
</template>
</div>
</template>
<script>
import { calculateDuration } from "../../helpers/time.js";
import { liveDuration } from "../../helpers/time.js";
import moment from "moment";
export default {
name: "Stages",
props: ["showgraph"],
data() {
return {
job_execs: []
};
},
在 Stages 组件中单击 charticon 时,如何隐藏 StageExecutionTimes 组件?
另外,我可以在不触及 StageExecutionTimes 组件的情况下完成此操作吗?似乎我可以将事件从 Stages 传递到主要组件,然后从那里隐藏 StageExecutionTimes。
解决方案
如果您有许多组件要共享数据,则需要一个 vuex 存储。您单击然后更改商店中的数据,然后在您使用 v-if="isShow"
@click="hideOtherComponent"
hideOtherComponent(){
this.store.commit('HIDE_COMPONENT');
}
computed: {
isShow() {
return store.state.StageExecutionTimesShow
}
}
<component v-if="isSHow">
` vuex
推荐阅读
- react-native - 如何在反应原生,原生基础中制作卡片圆形
- spring-boot - Undertow 是否能够使用相同的 IO 工作线程来处理两个不同的请求?
- cordova - Cordova - InApp Broswer - 没有后退按钮
- c++ - 当我们在 c++ 中使用 k=k-- 时会发生什么?
- protractor - 如何通过量角器脚本进行图像裁剪
- python - 匹配 Python 字典中不完整的字符串
- nginx - 安装 Laravel Valet 后,我可以在 Nginx 中拥有非 Laravel 站点吗?
- r - 如何准备数据以从数据框的 2 列创建分组直方图
- swift - 收到致命错误:双精度值无法转换为 Int,因为它是无限的或 NaN
- r - 软件包安装 - dplyr