Answer the question
In order to leave comments, you need to log in
Error ymaps (Yandex.Maps) when rewriting to Composition API Vue3?
Good afternoon, I ran into such a problem that when rewriting a component:
<template>
<div id="map" />
</template>
<script>
import { mapGetters } from "vuex";
export default {
name: "Map",
data: () => ({
initCoordinate: [55.670559, 37.609218],
}),
created() {
ymaps.ready(this.initMap);
},
computed: {
...mapGetters(["bigCardsList"]),
bigCardsListLength() {
return this.bigCardsList.length;
},
},
watch: {
bigCardsListLength(newCountOfLength) {
newCountOfLength
? this.bigCardsList.forEach((city) => {
this.addNewPlacemark(
[city.coordinates.latitude, city.coordinates.longitude],
city.city
);
this.map.panTo([
city.coordinates.latitude,
city.coordinates.longitude,
]);
})
: this.map.panTo(this.initCoordinate);
},
},
methods: {
initMap() {
this.map = new ymaps.Map("map", {
center: this.initCoordinate,
zoom: 10,
controls: [],
});
},
addNewPlacemark(coordinates, hintContent) {
this.map.geoObjects.add(
new ymaps.Placemark(coordinates, {
hintContent: hintContent,
})
);
},
},
};
</script>
<template>
<div id="map" />
</template>
<script>
import { computed, ref, watch } from "vue";
import { useStore } from "vuex";
export default {
name: "Map",
setup() {
const store = useStore();
const map = ref(null);
const initCoordinate = ref([55.670559, 37.609218]);
function initMap() {
map.value = new ymaps.Map("map", {
center: initCoordinate.value,
zoom: 10,
controls: [],
});
}
ymaps.ready(initMap);
const bigCardsList = computed(() => store.getters.bigCardsList);
const bigCardsListLength = computed(() => bigCardsList.value.length);
function addNewPlacemark(coordinates, hintContent) {
map.value.geoObjects.add(
new ymaps.Placemark(coordinates, {
hintContent: hintContent,
})
);
}
watch(
() => bigCardsListLength.value,
(newCountOfLength) => {
newCountOfLength
? bigCardsList.value.forEach((city) => {
addNewPlacemark(
[city.coordinates.latitude, city.coordinates.longitude],
city.city
);
map.value.panTo([
city.coordinates.latitude,
city.coordinates.longitude,
]);
})
: map.value.panTo(initCoordinate.value);
}
);
},
};
</script>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question