C
C
Comnox2021-09-21 10:28:37
Yandex maps
Comnox, 2021-09-21 10:28:37

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>


on the Composition Api in this format, it disappears as I understood the context for the map, since in the previous version I initialize it through this.map, and in the format through ref , something goes wrong:

614989676007e368458265.png

<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 question

Ask a Question

731 491 924 answers to any question