关于前端:vue3组件实列上下文获取问题

5次阅读

共计 488 个字符,预计需要花费 2 分钟才能阅读完成。

getCurrentInstance

获取以后组件实例

import {getCurrentInstance} from "vue";
const instance = getCurrentInstance();

ctx

以后组件的上下文,只能在开发环境下应用,生产环境下的 ctx 将拜访不到,ctx 中蕴含了组件中由 ref 和 reactive 创立的响应式数据对象,以及 proxy 下的属性

const {ctx} = getCurrentInstance();
  • 留神:在 setup 中不能够调用 getCurrentInstance().ctx 来获取组件外部数据,因为在 prod 模式会被干掉
  • 起因:

    • ctx 只是为了便于在开发模式下通过控制台查看
    • 在 prod 模式是一个空对象


图片起源掘金 春去春又来

proxy

在开发环境以及生产环境下都能放到组件上下文对象(举荐)

蕴含属性 $attrs,$data,$el,$emit,$forceUpdate,$nextTick,$options,$parent,$props,$refs,$root,$slots,$watch

const {proxy} = getCurrentInstance();
正文完
 0