Parent component :
When clicked, the dialog map component opens and updates the values that need to be passed
function editPowerStationChooseMap() {
mapVisible.value = true;
mapForm.address = null
mapForm.fullDddress = editPowerStationForm.value.address;
editAddressInfo.value = editPowerStationForm.value.address;
}
<el-dialog v-model="mapVisible" title="选择位置" width="70%">
<template #header>
<el-form :inline="true" ref="mapFormRef" :model="mapForm">
<el-form-item label="省市区" prop="address">
<el-cascader v-model="mapForm.address" :filterable="true" :options="addressOption.option" :props="cascaderProps" class="custom-cascader" @change="chooseAddress" style="width: 240px"/>
</el-form-item>
<el-form-item label="详细地址" prop="fullDddress">
<el-input v-model="mapForm.fullDddress" placeholder="请输入详细地址" style="min-width: 400px"/>
</el-form-item>
</el-form>
</template>
<Map @mapReady="handleMapReady" :addressInfo="addressInfo":editAddressInfo="editAddressInfo"/>
<span slot="footer" class="dialog-footer">
<el-button @click="mapVisible = false">取消</el-button>
<el-button type="primary" @click="confirmAddress">确定</el-button>
</span>
</el-dialog>
children component:
const emit = defineEmits(["mapReady"]);
const { addressInfo, editAddressInfo } = defineProps({
addressInfo: {
type: String,
},
editAddressInfo: {
type: String
}
});
watch([() => addressInfo, () => editAddressInfo], ([newAddress, newEditAddress], [oldAddress, oldEditAddress]) => {
let addressToGeocode = '';
// 判断是哪个值发生了变化
if (newAddress !== oldAddress) {
addressToGeocode = newAddress;
} else if (newEditAddress !== oldEditAddress) {
addressToGeocode = newEditAddress;
}
if (addressToGeocode) {
geocoder.getLocation(addressToGeocode, function (status, result) {
if (status === 'complete' && result.info === "OK") {
const lnglat = result.geocodes[0].location;
map.setCenter(lnglat);
positionPicker.on('success', function (positionResult) {
detailedAddress.value = positionResult.address;
emit("mapReady", {
map,
geocoder,
detailedAddress,
positonInfo: [positionResult.position.lng, positionResult.position.lat]
});
});
positionPicker.on('fail', function (positionResult) {
});
positionPicker.start(lnglat);
} else {
console.error('根据地址查询位置失败');
}
});
}
});
onMounted(() => {
window._AMapSecurityConfig = {
securityJsCode: '61ed75a46aa0cc6a2860aa025fd22ed5',
};
AMapLoader.load({
key: '9c4e9341d7408b1241277ae9d7498df9',
version: "2.0",
AMapUI: {
version: "1.1"
}
})
.then((AMap) => {
map = new AMap.Map("container", {
viewMode: "2D",
zoom: 11,
center: [116.397428, 39.90923], // 默认中心点
});
// 拖拽选址插件
AMapUI.loadUI(['misc/PositionPicker'], function (PositionPicker) {
positionPicker = new PositionPicker({
mode: 'dragMarker',//设定为拖拽地图模式,可选'dragMap'、'dragMarker',默认为'dragMap'
map: map, //依赖地图对象
iconStyle: {//自定义外观
url: ikun,
size: [48, 48], //要显示的点大小,将缩放图片
ancher: [24, 40],//锚点的位置,即被size缩放之后,图片的什么位置作为选中的位置
}
});
});
// Geocoder 插件
AMap.plugin('AMap.Geocoder', () => {
geocoder = new AMap.Geocoder({city: '全国'});
emit("mapReady", {map, geocoder, detailedAddress: ''});
});
})
.catch((e) => {
console.error(e);
});
});
When I click, the dialog opens but the watch code does not execute. When I add the delayer, the code executes normally. like this:
function editPowerStationChooseMap() {
mapVisible.value = true;
setTimeout(()=>{
mapForm.address = null
mapForm.fullDddress = editPowerStationForm.value.address;
editAddressInfo.value = editPowerStationForm.value.address;
},500)
}
Trying nextTick didn’t work either, was it because the map component took so long to load?