vue.js - how to get the component instance within an @click event? -
i have button in vue component
<el-button class='range-right' @click='deleteitem(item)'>delete</el-button>
within it's handler want invoke other methods of component. however, though can call deleteitem
component method, cannot @ actual component other methods.
is there way pass $component
param @click
event?
methods { deleteitem: (item, obj) => { let api = '/api/train/deleteitem?_id=' + item._id let resource = vue.resource(api) let vm = // not component window.delitem = console.log('deleteitem.this', this) resource.delete(api) .then(items => { console.log('deleted') vm.methods.load() //<< fails }) },
i think problem arrow function commented
see example here using function
:
https://jsfiddle.net/rvp6fvwp/
arrow function bound parent context said here https://vuejs.org/v2/guide/instance.html#properties-and-methods
don’t use arrow functions on instance property or callback (e.g.
vm.$watch('a', newval => this.mymethod()))
. arrow functions bound parent context, not vue instance you’d expect , this.mymethod undefined.
Comments
Post a Comment