Answer the question
In order to leave comments, you need to log in
Why does a hook prevent the component from reappearing?
Good evening. When curtain is true, the section is rendered. The same section has refs attached, which works to hide the section again by calling the function in the update hook. When curtain is true again, the section is not rendered.
I suspect that the hook is not working correctly.
Error:
Uncaught (in promise) TypeError: Cannot read properties of null (reading 'addEventListener')
at Proxy.updated (CarList.vue?9e02:22)
<template>
<section v-if='curtain' ref='scroll'>
</section>
</template>
<script>
import {mapGetters, mapMutations} from 'vuex';
export default {
name: 'CarList',
data () {
return {
}
},
updated() {
this.$refs.scroll.addEventListener('wheel', this.fadeOut);
},
methods: {
...mapMutations(['curtainFalse']),
fadeOut(e) {
if (e.deltaY < -5 && window.scrollY == 0) {
this.curtainFalse();
this.changeBackFalse();
}
console.log(this.curtain);
}
},
computed: {
...mapGetters(['curtain']),
}
}
</script>
Answer the question
In order to leave comments, you need to log in
updated() {
this.$refs.scroll.addEventListener('wheel', this.fadeOut);
},
v-if
false, there is no element. Accordingly, $refs.scroll
instead of a reference to a non-existent element, it is written null
, and you are trying to access the property of this null
, which you should not do - there null
are no properties and cannot be, such accesses end in errors like the one you got.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question