javascript - 从另一个元素实例引用 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 组件?
解决方案
像下面这样绑定这个实例 -
this.map.markers.user.on("moveend", function(e) {
this.showSubmit(e);
}.bind(this));
推荐阅读
- python - 如何使用 python 从网站中提取数据以获取数据表?
- javascript - Javascript Date.UTC() 返回前一天
- python - 读取行直到某个字符串并将其删除
- sql - 使用 CTE 向树数据添加排序覆盖
- angular - 基于使用 iif 运算符的条件使用 RxJS 进行嵌套 API 调用,导致超出最大调用堆栈大小
- sql - 如何解析具有多个下划线和破折号的字符串
- scala - Scala 中的深度存根
- java - 我无法说出我在这个 java 递归问题上做错了什么!我在下面发布了练习和我的代码
- sql - 在 SQL 中计算百分比聚合计数
- javascript - React 组件 - onClick 没有任何反应,这是为什么呢?