JLLMNCHR
JLLMNCHR

Reputation: 1571

Calling outside method inside a VUE.JS component

I want to ask you if it is possible, inside a VUE.JS component 'A', to call a method of another VUE.JS component 'B' that uses component 'A'.

Thank you

Upvotes: 0

Views: 52

Answers (1)

innerurge1
innerurge1

Reputation: 728

You can do this using $emit. https://v2.vuejs.org/v2/guide/components-custom-events.html

For example say your child componet has a delete button to delete it from the parent page:

<span title="remove" v-on:click="$emit('delete')">x</span>

Here I have used v-on:click to emit a custom event up to the parent component. https://v2.vuejs.org/v2/guide/events.html

In the parent component we listen for this event and run some function defined there when the event occurs:

<your-componet @delete="removeMe"/>

(@ is shorthand for the v-on: directive)

In your methods prop in the parent componet you would define the removeMe function:

methods :{
  removeMe () {
   // code to delete
  }
}

Upvotes: 2

Related Questions