简体   繁体   English

如何在父事件发生后调用子(或任何其他)组件中的方法?

[英]How to call a method in a child (or any other) component after an event in the parent?

A question like this:像这样的一个问题:
How is it possible in vue3 after an event to call a method in another component?在一个事件之后如何在vue3中调用另一个组件中的方法?
Found this solution for vue2为 vue2 找到了这个解决方案
this.$refs.myComponent.myMethod()
But how is it supposed to work without "this" in vue3 + composition api?但是在 vue3 + 组合 api 中,如果没有“这个”,它应该如何工作?

If this is not possible then what are the alternatives?如果这是不可能的,那么有什么替代方案?
The event itself occurs in the following code:事件本身发生在以下代码中:
(for example, we can take the resize event of a component and set a task - after it, activate a method inside another component) (例如,我们可以获取一个组件的 resize 事件并设置一个任务 - 在它之后,激活另一个组件内的方法)

 <Vue3DraggableResizable class="chartApp"
     @resizing="print('resizing')"
   >

How is it possible to implement it?怎么可能实现它?

Not sure about if they are best practices but here we go.不确定它们是否是最佳实践,但在这里我们是 go。

  1. I think you can watch your props change and trigger an event like this我认为你可以看到你的道具发生变化并触发这样的事件

    import { watch, toRefs } from 'vue' const props = defineProps({ yourReactiveProp: Boolean }) const { yourReactiveProp } = toRefs(props) // this is reactive watch( () => yourReactivePropn.value, (newVal) => { yourMethod() // you can pass the new value also. })
  2. You can use mitt package.您可以使用手套package。 Literally, you can listen and fire emit from anywhere to anywhere.从字面上看,您可以从任何地方到任何地方收听和发射。 Ie, parent-child, child-parent, siblings, non-siblings, etc..即,父子,子父,兄弟姐妹,非兄弟姐妹等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM