Des*_*uta 1 javascript google-maps vue.js nuxt.js
我在 SPA 模式的 Nuxt.js 应用程序中使用 Google Maps API,我想从 gmap 单击事件中调用 Vue.js 方法中的函数
我尝试了通常的this.createInfoWindow()
,但this
不是VueComponent
,而是Window
。
在我的组件中,我初始化了谷歌地图并在安装的 vue 中添加了点击事件:
async mounted() {
// Map initalization
const google = await gmapsInit()
this.map = new google.maps.Map(
document.getElementById('g-map'),
this.mapOptions
)
// Add click event
google.maps.event.addListener(this.map, 'click', e => {
// function call not working
this.createInfoWindow(e.latLng)
})
}
Run Code Online (Sandbox Code Playgroud)
在 vue 方法中,我有以下功能:
methods: {
async createInfoWindow(latLng) {
const google = await gmapsInit()
const infoWindow = new google.maps.InfoWindow({
content: 'InfoWindow',
position: latLng
})
infoWindow.open(this.map)
}
}
Run Code Online (Sandbox Code Playgroud)
一切似乎都在工作,除了函数调用: this.createInfoWindow(e.latLng)
如何createInfoWindow
从单击事件调用该函数?
正如您所说,this
在单击处理程序中不引用您的 Vue 实例。使用闭包。
// Add click event
const that = this
google.maps.event.addListener(this.map, 'click', e => {
// function call not working
that.createInfoWindow(e.latLng)
})
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2910 次 |
最近记录: |