B
B
Beetle2019-09-24 20:12:01
Vue.js
Beetle, 2019-09-24 20:12:01

Nuxt.js and $ref, why doesn't it see my ref?

mounted() {
  let timer;
  window.addEventListener('scroll', () => {
    if(timer) clearTimeout(timer);
    timer = setTimeout(() => {
      let testHeight = this.$refs.testElem.offsetHeight;
    });
  });
}

Added to mounted()
When moving from another page to a page with this code. gives an error
Uncaught TypeError: Cannot read property 'offsetHeight' of undefined
But continues to work correctly.
How to fix this error?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Daniil Bratukhin, 2019-09-24
@Jukk

Once it works, just check this.$ref.testElem for existence. And do not forget to delete handlers in beforeDestroy, otherwise the browser will send you events to the destroyed components :)

D
Dima Pautov, 2019-09-24
@bootd

show the code from the elements in the template
or looking ahead, do this
this.$refs.testElem.$el.offsetHeight

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question