Правильно ли обращаться к window и document во vue?

Добрый вечер.
Правильно ли обращаться к window и document во vue? Если нет, то как правильно это реализовать?
Пример кода:
// ...
  mounted: function () {
    this.$nextTick(function () {
      this.resize()
    })
    window.addEventListener('resize', this.resize) // 1
  },
  beforeDestroy: function () {
    window.removeEventListener('resize', this.resize) // 2
  },
  methods: {
    resize: function () {
      this.$store.dispatch('game/setMapSize', document.body.offsetWidth) // 3
    }
  },
// ...
  • Вопрос задан
  • 7070 просмотров
Решения вопроса 1
@de1m
Ну вы вроде всё правильно сделали, целиком выгдлядит как-то так должно:

var app = new Vue({
  el: '#app',
  data: function() {
    return {
      msg: 'Hello World! This is a Event listener test.',
      windowWidth: 0,
      windowHeight: 0,
    }
  },

  mounted() {
    this.$nextTick(function() {
      window.addEventListener('resize', this.getWindowWidth);
      window.addEventListener('resize', this.getWindowHeight);

      //Init
      this.getWindowWidth()
      this.getWindowHeight()
    })

  },

  methods: {
    getWindowWidth(event) {
        this.windowWidth = document.documentElement.clientWidth;
      },

      getWindowHeight(event) {
        this.windowHeight = document.documentElement.clientHeight;
      }
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.getWindowWidth);
    window.removeEventListener('resize', this.getWindowHeight);
  }
});
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы