首页 > 解决方案 > 从另一个元素实例引用 Vue 实例

问题描述

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

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

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

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

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

标签: javascriptvue.jsleaflet

解决方案


像下面这样绑定这个实例 -

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

推荐阅读