2.4.2
No reproduction link, need hot reload...
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.
After hot reload, display the width of the div
.
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
.
That happens because you have a setInterval
still running, you should clear it in beforeDestory:
<template>
<div>
<div ref="test"></div>
</div>
</template>
<script>
export default {
name: 'test',
mounted () {
this.interval = setInterval(() => {
console.log(this.$refs.test)
}, 2000)
},
beforeDestroy () {
clearInterval(this.interval)
}
}
</script>
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
Can confirm I am not using setInterval
anywhere and still having this issue.
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.
Im having the same issue. It was working in vue 2.5.17
Same issue here.
FYI, the response from @posva is correct; it was because setInterval
was still running.
Most helpful comment
Wait. This is not about interval, he just want to show that
$refs.key
will beundefined
. 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