我正在使用Google Maps API,并创建了一个组件来显示地图。在外部脚本加载后运行Vue组件的方法
的index.html:
<!DOCTYPE html>
<html>
<head>
...
</head>
<body>
<div id="app"></div>
<!-- built files will be auto injected -->
<script>
window.show_google_map = false;
console.log(window.show_google_map);
function initMap() {
console.log('map loaded');
window.show_google_map = true;
console.log(window.show_google_map);
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=googleapikeyxxxx&callback=initMap"
type="text/javascript"></script>
</body>
</html>
GoogleMap.vue:
<template>
<div class="google_map" :id="map_name"></div>
</template>
<script>
export default {
name: 'google_map',
props: ['map_id'],
data() {
return {
map_name: this.map_id + '-map',
}
},
methods: {
create_map() {
const element = document.getElementById(this.map_name);
const options = {
zoom: 14,
center: new google.maps.LatLng(51.501527, -0.1921837)
};
const map = new google.maps.Map(element, options);
const position = new google.maps.LatLng(51.501527, -0.1921837);
const marker = new google.maps.Marker({
position,
map
});
}
},
mounted() {
this.create_map();
}
}
</script>
的问题是,在加载谷歌地图API之前该组件呈现。如何在Google Maps API加载后显示?
是的,我想这样的。但是,计算的属性在加载时不会触发。我更新了我的问题。请看一看。 – Kakar
哦,我没有提出一个计算属性。而是一个观察者。它与计算的属性不同,它是双向的。 –
您无法观看未定义的变量。 – Reiner