0

I have created a leaflet map with vue.js. I have a method named 'showSubmit, which is to be called at leaflet marker moveend event. This is what I am doing:

this.map.markers.user.on("moveend", function(e) {
    this.showSubmit(e);
}); 

However, this call is showing error, as 'this' within the function refers to the leaflet map instance and not the vue instance. As a workaround, I have declared a variable, like this:

var $this = this;
this.map.markers.user.on("moveend", function(e) {
        $this.showSubmit(e, $this);
});

Although this works, but I want to avoid this approach. How can I access the vue component from within leaflet map instance?

2 Answers 2

2

Bind this instance like the following -

this.map.markers.user.on("moveend", function(e) {
  this.showSubmit(e);
}.bind(this));
Sign up to request clarification or add additional context in comments.

Comments

1

If you can use ES6 features, than arrow functions will help, they dont change this.

this.map.markers.user.on("moveend", (e) => {
    this.showSubmit(e);
}); 

2 Comments

Shouldn't it be .on("moveend", (e) => {...}) ?
@nikoshr oh, sure, u are right :) Thats what u get when dont pay enough attention

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.