如何在Vue JS中使用Google Map的信息窗口中的@click触发功能? [英] How to trigger a function using @click inside infowindow of google maps in vue js?

查看:183
本文介绍了如何在Vue JS中使用Google Map的信息窗口中的@click触发功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前的代码是

addpolygon: function(e) {
      var vm = this;
      var point = {
        lat: parseFloat(e.latLng.lat()),
        lng: parseFloat(e.latLng.lng())
      };
      vm.coord.push(point);
      vm.replot();
      vm.marker = new google.maps.Marker({
        position: point,
        map: vm.map,
        icon: "/fred.png"
      });
      vm.infowindow = new google.maps.InfoWindow({
        content:"<a class=\"btn btn-danger\" @click.native=\"removePoint("+vm.markerid+)\">Remove</a>",
        maxWidth: 300
      });
      vm.bindInfoWindow(vm.marker, vm.map, vm.infowindow);
      vm.markers[vm.markerid] = {
        marker: vm.marker,
        point: point
      };
      vm.markerid++;
    },

单击删除"时,需要触发另一个功能删除点.

When I click on Remove, I need to trigger another function remove Point.

我将其定义为

removePoint: function(id) {
      alert("adsf")
    },

但是我无法使用上面的代码触发相同的操作.当我单击删除"按钮时,没有任何反应.关于相同的问题是什么?请帮我解决问题吗?

But I am not able to trigger the same using the above code. Nothing happens when I click on the button remove. What is the problem regarding the same. Please help me to have a solution?

推荐答案

新解决方案

使用普通的点击处理程序从InfoWindow调用全局方法.

`onclick="removePoint(${vm.markerId})"`

然后使用闭包从全局方法访问您的虚拟机.

Then use a closure to access your vm from the global method.

const vm = this window.removePoint = function(id) { vm.removePoint(id) }

const vm = this window.removePoint = function(id) { vm.removePoint(id) }

如果您有多个实例,则需要扩展此方法.

IF you have multiple instances, you will need to extend this approach.

这里有2个问题.

首先,修复与引号有关的语法错误.

First, fix the syntax error concerning the quote.

vm.markerid + ")\">Remove</a>"

更好的是,利用模板字符串来避免这种报价混乱.

Even better, take advantage of template strings to avoid this kind of quote insanity.

vm.infowindow = new google.maps.InfoWindow({ content:`
<a class="btn btn-danger" @click.native="removePoint(${vm.markerid})">Remove</a>`, maxWidth: 300 });

第二,vue模板中的任何功能始终在组件的范围之内.假定this.对象放在前面.因此,呼叫removePoint实际上是呼叫this.removePoint.

Second, any function inside a vue template is always within the scope of the component. Assume a this. object is placed in front. So calling removePoint is really calling this.removePoint.

在实例内部定义函数.

vm.removePoint = function(id) { console.log(`removing point ${id}...`) }

vm.removePoint = function(id) { console.log(`removing point ${id}...`) }

或确保您的组件选项在methods部分中定义了removePoint.

Or make sure your component options defines removePoint in the methods section.

如果使用诸如$window.removePoint(" + vm.markerId + ")". rel ="nofollow noreferrer"> https://www.npmjs.com/package/window-plugin .

You can also define removePoint globally (on the window object) and call $window.removePoint(" + vm.markerId + ")" from the template if using a plugin such as https://www.npmjs.com/package/window-plugin.

@click.native=\"$window.removePoint(" + vm.markerid ...

function removePoint(id) { console.log(`removing point ${id}...`) }

function removePoint(id) { console.log(`removing point ${id}...`) }

这篇关于如何在Vue JS中使用Google Map的信息窗口中的@click触发功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆