获取当前组件实例
import { getCurrentInstance } from "vue"; const instance = getCurrentInstance();
当前组件的上下文,只能在开发环境下使用,生产环境下的 ctx 将访问不到,ctx 中包含了组件中由 ref 和 reactive 建立的响应式数据对象,以及 proxy 下的属性
const { ctx } = getCurrentInstance();
缘由:vue
图片来源掘金 春去春又来react
在开发环境以及生产环境下都能放到组件上下文对象(推荐)
包含属性$attrs,$data,$el,$emit,$forceUpdate,$nextTick,$options,$parent,$props,$refs,$root,$slots,$watchpost
const { proxy } = getCurrentInstance();