首页 > 其他分享 >getCurrentInstance

getCurrentInstance

时间:2022-11-21 14:58:40浏览次数:69  
标签:对象 解构 getCurrentInstance 实例 proxy 组件

Vue2中,可以通过this来获取当前组件实例; 

Vue3中,在setup中无法通过this获取组件实例,console.log(this)打印出来的值是undefined。

在Vue3中,getCurrentInstance()可以用来获取当前组件实例  vue3官方文档解释

getCurrentInstance是一个function方法,getCurrentInstance()是一个对象,proxy也是一个对象。proxy是getCurrentInstance()对象中的一个属性,通过对象的解构赋值方式拿到proxy。 

getCurrentInstance只能在setup生命周期钩子中使用。 

ctx和proxy都是getCurrentInstance()对象中的属性,通过解构赋值的方式拿到。可以看到,2者有所区别。ctx是普通对象,proxy是Proxy对象。

标签:对象,解构,getCurrentInstance,实例,proxy,组件
From: https://www.cnblogs.com/niufang/p/16911362.html

相关文章