Beusebiu
Beusebiu

Reputation: 1523

Access method on component VueJs

I have a method in parent that add an element to an array and I am not sure how to use it in my component (add-new-element). Basically in that method I add a new element to array to reload the data from table.

Parent

   <add-new-element id="add-element" @isAdded="onAddElement" ></add-new-customer>

   onAddElement(newElement){
       console.log('Reload table');
       this.items.unshift(newElement);
   },

Child

data() {
    return{
        isAdded: false,
    };
},

Upvotes: 2

Views: 35

Answers (1)

Arc
Arc

Reputation: 1086

If you're wanting to have a component re-render when data changes, this is automatic already if set in your data.

To have a child component access a method of a parent, use $emit and listen for it on the parent.

PARENT:

<ChildComponent v-on:added="onAddElement"/>

CHILD:

method: {
     elementAdded () {
          this.$emit('added', myArgument)
     }
}

Upvotes: 1

Related Questions