vue地图加载报错:map container div not exist

参考文献:

vue地图加载报错:map container div not exist-CSDN博客

原因:

  1. 地图加载的id与html中不一致

<div id="container" style="width: 1000px; height: 700px;">

let map = new this.AMap.Map('myMap', {

center: 121.227577, 31.101471, // 中心点坐标

resizeEnable: true, // 是否监控地图容器尺寸变化

zoom: 10, // 初始化地图层级,可以理解为缩放比例

showMarker: true, // 定位成功后在定位到的位置显示点标记,默认:true

});

2.地图加载的时候dom结构还没生成,把initmap函数放到了created生命周期中加载

// 错误:

created(){

this.initMap()

}

// 改为

mounted(){

this.initMap()

}

3.地图容器所在html代码被隐藏掉了,或者之前的代码存在错误

<el-row v-if="form.attachmentInfoList.length != 0">

<el-col :span="24">

<el-form-item label="¸½¼þÐÅÏ¢">

<div class="demo-image__preview" v-for="item in form.attachmentInfoList" :key="item">

<el-image style="width: 100px; height: 100px; float:left" :src="item" :preview-src-list="form.attachmentInfoList"></el-image>

</div>

</el-form-item>

</el-col>

</el-row>

<el-row :gutter="20">

<el-col :span="24">

<div class="grid-content bg-purple">

<el-form-item label="">

<div id="mapDiv" style="width: 100%;height: 650px;border:1px solid #bfcbd9;padding: 8px;z-index: 0;"></div>

</el-form-item>

</div>

</el-col>

</el-row>

data() {

return {

form: {

// attachmentInfoList: \[\]

},

}

},

刚开始data中注销掉了form中的attachmentInfoList属性,在html中第一个row中有个判断

v-if="form.attachmentInfoList.length != 0"

此处form.attachmentInfoList为undefined,没有length属性,所以报错了

相关推荐
涛涛ing1 小时前
OpenAI Astra 泄露:零样本生成 3D 网页,前端开发者慌了吗?
前端
ssshooter1 小时前
现在网页都能提供 MCP 了?!
前端·人工智能·程序员
杉氧1 小时前
状态管理变迁史:为什么我们放弃了 Redux 选择 Zustand?
android·前端·react native
cidy_982 小时前
OpenCode 手动配置火山方舟 Agent Plan 教程
前端
鹏多多2 小时前
PC 网站接入微信登录,这 10 个坑我替你踩完了!
前端·javascript·vue.js
律宏阔2 小时前
微信小程序使用 Orval + OpenAPI 自动生成接口:Axios 兼容踩坑记录
前端·微信小程序
律宏阔2 小时前
微信小程序集成 TDesign 完整记录:解决 NPM packages not found
前端·微信小程序
律宏阔2 小时前
微信小程序 ECharts 瘦身实战:分包异步化 + componentPlaceholder 避开主包 2MB 限制
前端·微信小程序
夏天要喝冰可乐2 小时前
从 Idea 到开源插件:我用 Vibe Coding 做了「文章摆渡」
前端·ai编程·vibecoding
小小小小宇2 小时前
pi agent
前端