首页 > 其他分享 >Vue3组合式API之getCurrentInstance详解

Vue3组合式API之getCurrentInstance详解

时间:2024-02-21 09:02:00浏览次数:28  
标签:console log ctx getCurrentInstance API proxy Vue3 typeof

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也是一个对象。proxy是getCurrentInstance()对象中的一个属性,通过对象的解构赋值方式拿到proxy。 

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

1.在onMunted生命周期中打印getCurrentInstance

2.定义一个test方法,通过click事件触发方法

onMounted(() => {
  console.log(getCurrentInstance(), typeof getCurrentInstance());
});
function test() {
  console.log(getCurrentInstance(), typeof getCurrentInstance());
}

 

看到在function中是无法获取该实例的。

let { ctx } = getCurrentInstance();
console.log(ctx, typeof ctx);
let { proxy } = getCurrentInstance();
console.log(proxy, typeof proxy);

 

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

补充:Vue3中关于getCurrentInstance的大坑

开发中只适用于调试! 不要用于线上环境,否则会有问题!

解决方案:

方案1.

const instance = getCurrentInstance()
console.log(instance.appContext.config.globalProperties)

 

获取挂载到全局中的方法

方案2.

const { proxy } = getCurrentInstance()  

 

标签:console,log,ctx,getCurrentInstance,API,proxy,Vue3,typeof
From: https://www.cnblogs.com/Fooo/p/18024418

相关文章

  • 记录--源码视角,Vue3为什么推荐使用ref而不是reactive
    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助ref 和 reactive 是Vue3中实现响应式数据的核心API。ref 用于包装基本数据类型,而reactive用于处理对象和数组。尽管 reactive 似乎更适合处理对象,但 Vue3官方文档更推荐使用 ref。 我的想法,ref就......
  • Apipost forEach循环控制器如何使用
    最近,Apipost对自动化测试进行了优化,新增foreach控制器。这个新功能的引入为自动化测试带来了更高的效率和灵活性。本文将介绍Apipost的foreach控制器,解释其用途和优势,帮助您更好地利用这一功能提升自己的测试工作。什么是foreach控制器?Foreach控制器允许您在测试用例中迭代执行......
  • 关于vue3的h函数
    h(ElInput,{class:'w200ml8',placeholder:'关键字搜索',clearable:true,modelValue:formData.url_pattern,'onUpdate:modelValue':(val:string)=&......
  • Vite + Vue3 实现前端项目工程化
    原文地址:https://mp.weixin.qq.com/s/cgiLx6NsoCAnh-mcR5peQgVue3发布至今,周边的生态、技术方案已足够成熟,个人认为新项目是时候切换到Vite+Vue3了。今天就给大家操作一下这种技术方案实现前端工程化。1.初始化项目通过官方脚手架初始化项目第一种方式,这是使用vite命......
  • [975] Creating a POSTMAN Api | AWS API Gateway Passing Data to AWS Lambda
    ref:AWSLambdaFunctionURLsref:GuidetoAWSLambdaFunctionURLs1.CreateaLambdaFunctionaws->Services->Lambda2.CreateaFunctionURLConfiguration->FunctionURLThen,theFunctionURLisgeneratedandcanbeseenfromtheup......
  • 手把手教你使用Vite创建Vue3项目
    原文地址:https://zhuanlan.zhihu.com/p/654327710今天就来说说怎么创建第一个Vue3项目。并安装ElementPlus及一些常用配置,实现如下简单增删改查页面一、工具简介这里我们简单介绍一下文章中使用到的工具,使用这些工具可以提高我们开发效率。当然了只有nodejs 是必须要安装的......
  • Vue3引入element报错
    问题描述:在Vue3中引入element(控制台npmielement-ui-S),引入完成后并不能成功应用element组件解决方案:目前element-ui只支持Vue2.6以下的版本,想在Vue3.0使用这个组件库,就要使用element-plus1.在根目录vueaddelement-plus2.在main.js中引入即可import{createApp}fro......
  • 测试面试题3-解释什么是RESTful API?
    RESTfulAPI指的是基于REST架构风格设计的应用程序接口。REST(RepresentationalStateTransfer)是一种软件架构风格,它是一种设计风格而非标准。RESTful架构通常基于HTTP协议,提倡使用标准的HTTP方法(GET、POST、PUT、DELETE等)来实现资源的增删改查操作。RESTful架构的主要设......
  • Vue3入门
    认识Vue3目录认识Vue3Vue2选项式APIvsVue3组合式APIVue3的优势使用create-vue搭建Vue3项目认识create-vue使用create-vue创建项目熟悉项目和关键文件组合式API—setup选项setup选项的写法和执行时机setup中写代码的特点<scriptsetup>语法糖组合式API—......
  • vue3 自动滚动到底部
    <template><divref="container"class="container"><!--内容--></div></template><script>import{ref,watchEffect}from'vue';exportdefault{setup(){constcontain......