question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

this.$refs.key return undefined after hot reload

See original GitHub issue

Version

2.4.2

Reproduction link

No reproduction link, need hot reload…

Steps to reproduce

Use this vue file:

<template>
  <div>
    <div ref="test"></div>
  </div>
</template>

<script>
export default {
  name: 'test',
  mounted () {
    setInterval(() => {
      console.log(this.$refs.test)
    }, 2000)
  }
}
</script>

<style>
</style>

Run it then edit the file, change this.$refs.test to this.$refs.test.clientWidth, save and watch the console.

What is expected?

After hot reload, display the width of the div.

What is actually happening?

After hot reload, return undefined, then the width of the div.


It the same if you use a $refs in a function of an event listener. Even if you destroy it in deforeDestroy.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

8reactions
noomzcommented, Jan 16, 2019

Wait. This is not about interval, he just want to show that $refs.key will be undefined. It seems like after hot-reloading, reference on $refs.key is missing. I still have this problem with Vue CLI 3.0.0-rc.11

6reactions
gunduzemreozercommented, Mar 22, 2019

Yes, unfortunately I live this issue. I’am using this.$nextTick and also setTimeout, both of them are same result. I write console.log this.$refs on nextTick and it returns id of it in object key but the value is undefined.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Vue.js refs are undefined, even though this.$refs shows theyre ...
mapRef in mounted() tried to wrap it with setTimeout() and 2 layers of this.$nextTick() . It works only on hot reload, but no...
Read more >
[SOLVED] This.$refs.key returns undefined when it really is
I am using Vue 2.0 with Webpack. When I say console.log(this.$refs) (inside created() method inside a low level component) it returns:
Read more >
Focus management with Vue refs - Learn web development
Now when you activate the "Edit" button and then cancel or save your changes via the keyboard, focus should be returned to the...
Read more >
Understanding refs in Vue - LogRocket Blog
According to what we already know about Vue ref s, they return an object, but judging by the undefined response, something is wrong....
Read more >
useForm - watch - Simple React forms validation
When defaultValue is not defined, the first render of watch will return undefined because it is called before register . It's recommend to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found