从另一个元素实例引用Vue实例

时间:2018-06-22 07:37:50

标签: javascript vue.js leaflet

我已经用vue.js创建了一个传单地图。我有一个名为'showSubmit的方法,该方法将在传单标记moveend事件中调用。这就是我正在做的:

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

但是,此调用显示错误,因为函数中的“ ”是指传单地图实例,而不是vue实例。解决方法是,我声明了一个变量,如下所示:

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

虽然这可行,但是我想避免这种方法。如何从传单地图实例中访问vue组件?

2 个答案:

答案 0 :(得分:2)

绑定实例,如下所示-

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

答案 1 :(得分:0)

如果您可以使用ES6功能,那么arrow functions会有所帮助,他们不会对此进行更改。

$refs!: {
    helloComponent: Hello
}