非原創(chuàng),文章來(lái)源:https://www.cnblogs.com/jin-zhe/p/9985436.html
this.$nextTick()將回調(diào)延遲到下次 DOM 更新循環(huán)之后執(zhí)行。在修改數(shù)據(jù)之后立即使用它,然后等待 DOM 更新。它跟全局方法 Vue.nextTick 一樣,不同的是回調(diào)的 this 自動(dòng)綁定到調(diào)用它的實(shí)例上。
假設(shè)我們更改了某個(gè)dom元素內(nèi)部的文本,而這時(shí)候我們想直接打印出這個(gè)被改變后的文本是需要dom更新之后才會(huì)實(shí)現(xiàn)的,也就好比我們將打印輸出的代碼放在setTimeout(fn, 0)中;
<template>
<section>
<div ref="hello">
<h1>Hello World ~</h1>
</div>
<el-button type="danger" @click="get">點(diǎn)擊</el-button>
</section>
</template>
<script>
export default {
methods: {
get() {
}
},
mounted() {
console.log(333);
console.log(this.$refs['hello']);
this.$nextTick(() => {
console.log(444);
console.log(this.$refs['hello']);
});
},
created() {
console.log(111);
console.log(this.$refs['hello']);
this.$nextTick(() => {
console.log(222);
console.log(this.$refs['hello']);
});
}
}
</script>