Vue3.x中的核心方法,用于访问实例上下文的router及vuex等
使用说明:setup的执行时组件对象还没有创建,此时不能使用this来访问data/computed/methods/props
我们可以通过 getCurrentInstance这个函数来返回当前组件的实例对象,也就是当前vue这个实例对象
import { getCurrentInstance } from 'vue';setup(){// 获取当前组件实例const instance = getCurrentInstance();// 获取当前组件的上下文,下面两种方式都能获取到组件的上下文。const { ctx } = getCurrentInstance(); // 方式一,这种方式只能在开发环境下使用,生产环境 下的ctx将访问不到const { proxy } = getCurrentInstance(); // 方式二,此方法在开发环境以及生产环境下都能到组件上下文对象(推荐)// ctx 中包含了组件中由ref和reactive创建的响应式数据对象,以及以下对象及方法;proxy.$attrsproxy.$dataproxy.$elproxy.$emitproxy.$forceUpdateproxy.$nextTickproxy.$optionsproxy.$parentproxy.$propsproxy.$refsproxy.$rootproxy.$slotsproxy.$watch}