javascript - 孩子到父母到另一个孩子之间的Vue通信
问题描述
我有一个<payment-child-component>
处理所有订阅和付款的,我也有
其他<check-active-child-component>
我希望这两个组件进行通信。persay 在<payment-component>
用户取消他的订阅我想触发我有一个方法,<check-active-component>
其中调用checkActive()
所以从payment-component
发出到parent-component
订阅取消方法被触发然后触发checkActive()
里面的方法check-active-component
因此,如果我的逻辑是好的,那么确切的问题是:我如何从父组件触发方法到子组件?
解决方案
要从父组件调用子组件的方法,可以使用ref
. 这是一个例子:
子组件:
export default {
name: "ChildComponent",
methods: {
childMethod(){
console.log("hello from child");
}
}
};
父组件:
<template>
<div id="app">
<ChildComponent ref="myChild"/>
</div>
</template>
<script>
import ChildComponent from "./components/ChildComponent";
export default {
name: "App",
components: {
ChildComponent
},
mounted(){
this.$refs.myChild.childMethod()
}
};
</script>
推荐阅读
- python - Python tkinter 按钮事件
- html - 使用纯 css 的位置图标动画
- java - 如何获取歌曲的封面图片?
- python - 如何将此顺序模型更改为功能模型?
- maven - 使用 JDK 10 / JDK 11 和 maven pugin 进行 Proguard
- php - 带有用户输入值的复选框
- json - JSON:Spring Boot 条件验证
- amazon-web-services - 是否可以从订阅中提取 Cloudwatch 日志的内容
- java - 由 defaultValue 引起的将 int 从一个活动传递到另一个活动的问题
- selenium - .net 3.5 视觉工作室的硒