一、 vue中使用forceUpdate
作用:在Vue官方文檔中指出,$forceUpdate具有強(qiáng)制刷新的作用,迫使vue實(shí)例重新(rander)渲染虛擬dom,注意它僅僅影響實(shí)例本身和插入插槽內(nèi)容的子組件,而不是所有子組件。
在vue2中使用文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-696048.html
this.$forceUpdate()
vue3中使用
import { getCurrentInstance } from 'vue' // 先引入
setup(){
// 解構(gòu)賦值 設(shè)置別名that,也可不寫(xiě):that,直接ctx
let { ctx: that } = getCurrentInstance()
that.$forceUpdate()
}
這樣使用的時(shí)候會(huì)報(bào)錯(cuò) Property ‘ctx’ does not exist on type ‘ComponentInternalInstance | null’.
萬(wàn)惡的類(lèi)型檢測(cè)
const { ctx: that } = getCurrentInstance() as any
二、vue中使用$nextTick
作用:vue中的nextTick()是在下次 DOM 更新循環(huán)結(jié)束之后執(zhí)行延遲回調(diào),在修改數(shù)據(jù)之后使用$nextTick(),則可以在回調(diào)中獲取更新后的 DOM。
在vue2中使用
this.visible = false
// 直接使用
this.$nextTick(() => {
// 要執(zhí)行的方法
this.visible = true
})
在vue3中使用文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-696048.html
import { nextTick } from 'vue' // 先引入
setup () {
const visible = ref<boolean>(false)
// 使用
nextTick(()=>{
visible.value = true
})
return {
visible
}
}
到了這里,關(guān)于vue vue3中使用nextTick和forceUpdate及注意事項(xiàng)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!