vue.jsvuejs2vue-componentvue-tables-2

Force refresh of VueTable2 on child component


I have a customer component, and inside of it there is a child component with a vuetable 2. What I need to do is to refresh the table, when I update or create a new customer on parent component.

I was looking to use $emit on parent:

this.$emit('CustomerInform');

In vuetable2 component:

this.$on('CustomerInform', () => {
   this.$refs.CustomersTable.refresh();
});

When the table was in the same componen where I made the POST/PUT with "$refs.CustomersTable.refresh()" all was working fine...


Solution

  • One way to do it would be to put a ref on the child component inside the parent:

    <customers-table ref="table" />
    

    Then, you will have access to its methods and attributes inside the parent, so you could simply do:

    this.$refs.table.$refs.CustomersTable.refresh()
    

    Another way to do it, is to use a global event bus.

    EventBus.js:

    import Vue from 'vue';
    export const EventBus = new Vue();
    

    Parent:

    import { EventBus } from EventBus
    
    // ...
    
    EventBus.$emit('CustomerInform')
    

    Child:

    import { EventBus } from EventBus
    
    // ...
    
    created () {
         EventBus.$on('CustomerInform', () => {
            this.$refs.CustomersTable.refresh();
         });
    }