SoFunction
Updated on 2025-04-12

The solution record of vue cannot get element for the first time

Preface

The solution to not obtaining elements for the first time in vue, whether to click the pop-up button for the first time to get the elements in the pop-up window? Open the pop-up window to get the element is empty and it will take a second time to get it.

1. Get it after updating the dom

this.$nextTick(callback)
methods: {
    play() {
        //Get the element        ($('#video'));
        this.$nextTick(function() {
            //Get the element            ($('#video'));
        });
    }
}

2. Obtain through timer

setTimeOut(fn, 0)
methods:{
    play() {
        //Get the element        ($('#video'));
        setTimeOut(function(){
            //Get the element            ($('#video'));
        }, 0);
    }
}

3. Get it by triggering the event

@opened
<el-dialog @opened="play"></el-dialog>
 
methods: {
    play() {
        //Get the element        ($('#video'));
    }
}

Summarize

This is the end of this article about the solution to the first time that vue cannot obtain elements. For more related contents of vue cannot obtain elements, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!