Vue3组合式API的getCurrentInstance方法具体如何获取当前组件实例的详细信息?
- 内容介绍
- 文章标签
- 相关推荐
本文共计403个文字,预计阅读时间需要2分钟。
在Vue2中,可以通过`this`来获取当前组件实例;而在Vue3中,`setup`函数中无法通过`this`获取组件实例,但可以使用`getCurrentInstance()`来获取。
Vue2中,可以通过this来获取当前组件实例;
Vue3中,在setup中无法通过this获取组件实例,console.log(this)打印出来的值是undefined。
在Vue3中,getCurrentInstance()可以用来获取当前组件实例vue3官方文档解释
let { proxy } = getCurrentInstance();
在setup中分别打印下面3个值,结果如下:
console.log(getCurrentInstance,typeof(getCurrentInstance)); console.log(getCurrentInstance(),typeof(getCurrentInstance())); console.log(proxy,typeof(proxy));
可以看到,getCurrentInstance是一个function方法,getCurrentInstance()是一个对象,proxy也是一个对象。
本文共计403个文字,预计阅读时间需要2分钟。
在Vue2中,可以通过`this`来获取当前组件实例;而在Vue3中,`setup`函数中无法通过`this`获取组件实例,但可以使用`getCurrentInstance()`来获取。
Vue2中,可以通过this来获取当前组件实例;
Vue3中,在setup中无法通过this获取组件实例,console.log(this)打印出来的值是undefined。
在Vue3中,getCurrentInstance()可以用来获取当前组件实例vue3官方文档解释
let { proxy } = getCurrentInstance();
在setup中分别打印下面3个值,结果如下:
console.log(getCurrentInstance,typeof(getCurrentInstance)); console.log(getCurrentInstance(),typeof(getCurrentInstance())); console.log(proxy,typeof(proxy));
可以看到,getCurrentInstance是一个function方法,getCurrentInstance()是一个对象,proxy也是一个对象。

