The usage of setTimeout in nextTick and JS in vue

problem description

in vue, I have the following templates:

<i class="bar" :style="style">
    <i class="dot"></i>
</i>

then the code in js is as follows:

props: {
        progress: {
            type: Number,
            default: function () {
                return 0;
            }
        },
        userInfo: {
            type: Object,
            default: function () {
                return {};
            }
        }
    },
    mounted() {
        let self = this;
        self.$nextTick(() => {
            self.width = self.progress;
        })
        // setTimeout(() => {
        //     this.width = this.progress;
        // })

    },
    computed: {
        style() {
            return {
                width: this.width + "%",
            }
        }
    },
The style of

bar has a dynamic effect of width change:

transition: width 0.5s ease;

Why do I have no dynamic effect when I use nextTick, but why do I have dynamic effect when I use setTimeout?

Jan.12,2022

is quite detailed


nextTick: delays the callback until after the next DOM update cycle
setTimeout: delays the callback to after the specified time
the prerequisites for both callbacks are different

Menu