vue vue3中使用nextTick和forceUpdate及注意事项

一、 vue中使用forceUpdate

作用:在Vue官方文档中指出,$forceUpdate具有强制刷新的作用,迫使vue实例重新(rander)渲染虚拟dom,注意它仅仅影响实例本身和插入插槽内容的子组件,而不是所有子组件。

在vue2中使用

this.$forceUpdate()

vue3中使用

import { getCurrentInstance } from 'vue' // 先引入
setup(){
// 解构赋值 设置别名that,也可不写:that,直接ctx
  	let { ctx: that } = getCurrentInstance()
	that.$forceUpdate()
}

这样使用的时候会报错 Property ‘ctx’ does not exist on type ‘ComponentInternalInstance | null’.

万恶的类型检测

const { ctx: that } = getCurrentInstance() as any

二、vue中使用$nextTick

作用:vue中的nextTick()是在下次 DOM 更新循环结束之后执行延迟回调,在修改数据之后使用$nextTick(),则可以在回调中获取更新后的 DOM。

在vue2中使用

this.visible = false
// 直接使用
this.$nextTick(() => {
   // 要执行的方法
	this.visible = true
})

在vue3中使用

import { nextTick } from 'vue' // 先引入
setup () {
	const visible = ref<boolean>(false)
	// 使用
	nextTick(()=>{
		visible.value = true
	})
	return {
		visible
	}
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值