简体   繁体   English

Vue.js - 如何从另一个组件调用方法

[英]Vue.js - How to call method from another component

I am using Vue.Js v2.我正在使用 Vue.Js v2。 I want to call component1->c1method in component2->c2method for reload data after submitting.提交后我想在component2->c2method中调用component1->c1method来重新加载数据。

Vue.component('component1', {
  methods: {
    c1method: function(){
     alert('this is c1method')
    },
  }
})
Vue.component('component2', {
  methods: {
    c2method: function(){
     component('component1').c1method()//like this
    },
  }
})

For non-parent-child relation, then this is the same as this one.对于非父子关系,则与此相同。 Call one method, apparently any method of a component from any other component.从任何其他组件调用一个方法,显然是组件的任何方法。 Just add a $on function to the $root instance and call form any other component accessing the $root and calling $emit function.只需将$on函数添加到$root实例并调用任何其他访问$root并调用$emit函数的组件。

On First component在第一个组件上

....
    mounted() {
        this.$root.$on('component1', () => {
            // your code goes here
            this.c1method()
        }
    }

and in the second component call the $emit function in $root并在第二个组件中调用$root中的$emit函数

...
    c2method: function(){
     this.$root.$emit('component1') //like this
    },

It acts more like a socket.它更像是一个套接字。 Reference here参考这里

https://stackoverflow.com/a/50343039/6090215 https://stackoverflow.com/a/50343039/6090215

// Component A Vue.component('A', { created() { this.$root.$refs.A = this; }, methods: { foo: function() { alert('this is A.foo'); } } }); // Component B Vue.component('B', { methods: { bar: function() { this.$root.$refs.A.foo(); } } });

No need for hacky solutions.无需骇人听闻的解决方案。
In vuejs we can create events that can be listened globally.在 vuejs 中,我们可以创建可以全局监听的事件。
With this feature, whenever we want to call our beloved function, we just emit this event.有了这个特性,每当我们想要调用我们钟爱的函数时,我们就发出这个事件。
Now, we just listen to this event all the time from the component.现在,我们只是一直从组件中监听这个事件。 whenever this global event happens we can execute our method we want to call.每当这个全局事件发生时,我们就可以执行我们想要调用的方法。

It's pretty simple:这很简单:

  1. you go to main.js, before creating the vue instance, write this:你去 main.js,在创建 vue 实例之前,这样写:
export const eventBus = new Vue(); // added line


new Vue({
    ...
    ...
    ...
    render: h => h(App)
}).$mount('#app');


  1. Anywhere we want to fire the target function, we dont fire it, we just emit this event:任何我们想触发目标函数的地方,我们不触发它,我们只是发出这个事件:
eventBus.$emit('fireMethod');
  1. Now in our component that has the target function, we always listen to this event:现在在我们的具有目标函数的组件中,我们总是监听这个事件:
created() {
    eventBus.$on('fireMethod', () => {
            this.myBelovedMethod();
    })
}

Dont forget to import eventBus in top.不要忘记在顶部导入 eventBus。

import {eventBus} from "path/to/main.js";

thats it, few lines of code, no hacky, all vuejs power.就是这样,几行代码,没有hacky,所有vuejs的力量。

The docs address this situation文档解决了这种情况

https://v2.vuejs.org/v2/guide/components.html#Non-Parent-Child-Communication https://v2.vuejs.org/v2/guide/components.html#Non-Parent-Child-Communication

If your components have the same parent, you can emit an event that the parent listens to.如果您的组件具有相同的父级,则可以发出父级侦听的事件。 Then with the ref property set, you can call the c1method from the parent.然后使用ref属性集,您可以从父级调用c1method

https://v2.vuejs.org/v2/guide/components.html#Child-Component-Refs https://v2.vuejs.org/v2/guide/components.html#Child-Component-Refs

Try this out.试试这个。

<template>
 ...
 <component1 ref='componentOne'>
...
</template>
<script>
  Vue.component('component2', {
    methods: {
     c2method: function(){
       this.$refs.componentOne.c1method();
     }
   }
  });
</script>

If anyone is looking for a solution in Vue.js v3:如果有人在 Vue.js v3 中寻找解决方案:

https://v3.vuejs.org/guide/migration/events-api.html#event-bus https://v3.vuejs.org/guide/migration/events-api.html#event-bus

https://github.com/developit/mitt#install https://github.com/developit/mitt#install

    import mitt from 'mitt'
    
    const emitter = mitt()
    
    // listen to an event
    emitter.on('foo', e => console.log('foo', e) )
    
    // listen to all events
    emitter.on('*', (type, e) => console.log(type, e) )
    
    // fire an event
    emitter.emit('foo', { a: 'b' })
    
    // clearing all events
    emitter.all.clear()

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

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