我使用的是Vue 3和Google Maps js API.

我的目标是点击一个标记,这会打开一个信息窗口,在那个窗口中有一个按钮,用户可以点击它来运行一些代码.

我制作了一个定制的HTML元素,使其位于单击标记时创建的Infowindow中.但是我不能在这个定制的html中引用我的函数.

如何在定制的html中引用我的函数?

<script setup>
/* eslint-disable no-undef */
import { computed, ref, onMounted, onUnmounted, watch, reactive, toRaw } from 'vue'
import { useMapStore } from '../pinia/map'

import { Loader } from '@googlemaps/js-api-loader'

const mapStore = useMapStore()

//~~ MAPPING ~ load API
const GOOGLE_MAPS_API_KEY = import.meta.env.VITE_GAPI
const loader = new Loader({
  apiKey: GOOGLE_MAPS_API_KEY,
  version: 'beta',
  libraries: ['marker']
})

const mapDiv = ref(null) // define divref to populate the map
let map = mapStore.map // get map from pinia store to persist map


const initMap = async () => {
  await loader.load()

  map = new google.maps.Map(mapDiv.value, {
    center: { lat: 40, lng: -80 },
    zoom: 4,
    maxZoom: 19,
    minZoom: 3
  })

  // create a new marker with custom content for the info window
  const marker = new google.maps.Marker({
    position: { lat: 40, lng: -80 },
    map: map,
    title: 'My Marker'
  })

  // create a new info window with custom HTML content
  const infoWindowContent = '<div><h3>My Marker Info Window</h3><button onclick="testFx()" >WORK</button></div>'
  const infoWindow = new google.maps.InfoWindow({
    content: infoWindowContent
  })

  // add an event listener to open the info window when the marker is clicked
  marker.addListener('click', () => {
    infoWindow.open(map, marker)
  })

}

//~ initialize map 
onMounted(async () => {
  await initMap()
})

const testFx = () =>{
  console.log('something worked');
}

</script>

<template>
  <div style="overflow-y: hidden">
    <div ref="mapDiv" style="background-color: #4CAF50; width: 100%; height: 90vh"></div>
  </div>
</template>

除了运行函数之外,这段代码还可以执行我需要的所有操作.我得到了这个错误:

Uncaught ReferenceError: testFx is not defined
    at HTMLButtonElement.onclick

推荐答案

我使用以下代码解决了这个问题:

    marker.addListener('click', () => {
      infoWindow.open(map, marker)

      // add event listener when InfoWindow content is ready
      google.maps.event.addListenerOnce(infoWindow, 'domready', () => {
        const button = document.getElementById('my-button')
        button.addEventListener('click', () => {

          console.log('PUT FUNCTION HERE')

        })
      })
    })

现在我可以引用Google Maps inowindow中的函数

Javascript相关问答推荐

我无法使用tailwind-css和reactJS修改图像的位置

如果没有尾随斜线,托管在收件箱中的React/Vite将无法工作

Vue Quill css仅应用于我的第一个Quill Editor组件+如何自定义工具栏

如何保持子画布元素的1:1宽高比?

获取加载失败:获取[.]添加时try 将文档添加到Firerestore,Nuxt 3

使用JavaScript在ionic Web应用程序中更新.pane和.view的背景 colored颜色

基于变量切换隐藏文本

colored颜色 检测JS,平均图像 colored颜色 检测JS

如何在Javascript中使用Go和检索本地托管Apache Arrow Flight服务器?

为什么按钮会随浮动属性一起移动?

在带有背景图像和圆形的div中添加长方体阴影时的重影线

闭包是将值复制到内存的另一个位置吗?

禁用.js文件扩展名并从目录导入隐式根index.js时,找不到NodeJS导入模块

Angular 形式,从DOM中删除不会删除指定索引处的内容,但会删除最后一项

如何在Bootstrap中减少网格系统中单个div的宽度

使用类型:assets资源 /资源&时,webpack的配置对象&无效

WebSocketException:远程方在未完成关闭握手的情况下关闭了WebSocket连接.&#三十九岁;

如何在FiRestore中的事务中使用getCountFromServer

react 路由如何使用从加载器返回的数据

如何在Java脚本中并行运行for或任意循环的每次迭代