M
M
mazahaler2019-10-21 14:20:41
JavaScript
mazahaler, 2019-10-21 14:20:41

Why Cannot read property 'classList' of undefined?

Hello, I'm listening to an event in a component and adding/removing a class from an element. The class is added and removed fine, but in the console the error is "Cannot read property 'classList' of undefined"
Here is the code:

created() {
            eventEmitter.$on('checkIfPickingStart', () => {
                    const target = this.$refs['compareBlock']
                    if (!this.currentSide) target.classList.add('pickingNotYetStarted')
                    setTimeout(() => (target.classList.remove('pickingNotYetStarted')), 1000)
                })
        }

What is the problem here?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2019-10-21
@bingo347

Move this code from the created hook to the mounted hook

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question