vue3中getCurrentInstance示例讲解

 更新时间:2023年03月23日 15:54:10   作者:程序猿向前跑  
这篇文章主要给大家介绍了关于vue3中getCurrentInstance的相关资料,文中还介绍了Vue3中关于getCurrentInstance的大坑,需要的朋友可以参考下

父组件中:

1.setup语法糖中导入子组件

2.在子组件标签上绑定ref值

3.setup内部从vue中按需导出 getCurrentInstance 方法

4.调用getCurrentInstance方法导出proxy

5.通过proxy.$refs.子组件ref名.子组件内属性/方法 实现调用

<template>
  <!-- 父组件 -->
  <div>
    <!-- 子组件 -->
    <Child ref="child" />
    <button @click="changeChildren">子组件count+1</button>
  </div>
</template>
 
<script setup lang="ts" name="Father">
import { getCurrentInstance, ComponetInternalInstance,ref } from "vue";
import Child from "./zi.vue";
const child = ref(null)
 // as ComponetInternalInstance表示类型断言,ts时使用。否则报错,proxy为null
const { proxy } = getCurrentInstance() as ComponetInternalInstance;
function changeChildren() {
  proxy.$refs.child.count += 1;
  //也可以使用ref数据.value的形式调用:
  //child.value.count += 1
  console.log(child.value.name)
}
</script>
 
<style scoped></style>

main.js

import api from "./utils/api.js"
import StringUtil from "./utils/StringUtil.js"

app.config.globalProperties.api = api;
app.config.globalProperties.StringUtil = StringUtil;
import {getCurrentInstance } from 'vue';

const { proxy } = getCurrentInstance();

console.log(proxy.api);
console.log(proxy.StringUtil.isBlank('1'));

方式一、通过 getCurrentInstance 方法获取当前组件实例,从而获取 route 和 router

Html

<template>
  <div>

  </div>
</template>
<script>
import { defineComponent, getCurrentInstance } from 'vue'

export default defineComponent({
  name: 'About',
  setup(){
    const { proxy } = getCurrentInstance()
    console.log(proxy.$root.$route)
    console.log(proxy.$root.$router)
    return {}
  }
})
</script>

方式二:通过从路由中导入 useRoute useRouter 使用 route 和 router。

Html

import { defineComponent } from ‘vue'
import { useRoute, useRouter } from ‘vue-router'
export default defineComponent({
setup () {
const $route = useRoute()
const r o u t e r = u s e R o u t e r ( ) c o n s o l e . l o g ( router = useRouter() console.log(router=useRouter()console.log(route)
console.log($router)
}
})

附:Vue3中关于getCurrentInstance的大坑

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

解决方案:

方案1.

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

获取挂载到全局中的方法

方案2.

const { proxy } = getCurrentInstance()  

使用proxy线上也不会出现问题

总结

到此这篇关于vue3中getCurrentInstance的文章就介绍到这了,更多相关vue3中getCurrentInstance内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Element-ui DatePicker显示周数的方法示例

    Element-ui DatePicker显示周数的方法示例

    这篇文章主要介绍了Element-ui DatePicker显示周数的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-07-07
  • axios对请求各种异常情况处理的封装方法

    axios对请求各种异常情况处理的封装方法

    今天小编就为大家分享一篇axios对请求各种异常情况处理的封装方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-09-09
  • Vue脚手架的创建超详解步骤

    Vue脚手架的创建超详解步骤

    这篇文章主要给大家介绍了关于Vue脚手架创建的相关资料,Vue脚手架是vue官方提供的标准化开发工具(平台),文中通过代码以及图文介绍的非常详细,需要的朋友可以参考下
    2024-01-01
  • VUE前端打包到测试环境的解决方法

    VUE前端打包到测试环境的解决方法

    这篇文章主要介绍了若依VUE前端打包到测试环境,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-12-12
  • Vue3+Vite 环境变量和模式配置详解(推荐)

    Vue3+Vite 环境变量和模式配置详解(推荐)

    在Vue 3中,你可以通过 import.meta.env 访问环境变量,这些变量可以在你的应用代码中使用,但它们通常用于配置不应该硬编码在代码中的值,这篇文章主要介绍了Vue3+Vite 环境变量和模式配置详解,需要的朋友可以参考下
    2024-12-12
  • 一文详解vue3项目实战中的接口调用

    一文详解vue3项目实战中的接口调用

    在企业开发过程中,往往有着明确的前后端的分工,前端负责接收、使用接口,后端负责编写、处理接口,下面这篇文章主要给大家介绍了关于vue3项目实战中的接口调用的相关资料,需要的朋友可以参考下
    2022-12-12
  • Vue中的slot使用插槽分发内容的方法

    Vue中的slot使用插槽分发内容的方法

    这篇文章主要介绍了Vue中的slot使用插槽分发内容的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-03-03
  • vue项目中的webpack-dev-sever配置方法

    vue项目中的webpack-dev-sever配置方法

    下面小编就为大家分享一篇vue项目中的webpack-dev-sever配置方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2017-12-12
  • vue自定义指令和动态路由实现权限控制

    vue自定义指令和动态路由实现权限控制

    这篇文章主要介绍了vue自定义指令和动态路由实现权限控制的方法,帮助大家更好的理解和学习vue,感兴趣的朋友可以了解下
    2020-08-08
  • vue数组动态刷新失败问题及解决

    vue数组动态刷新失败问题及解决

    这篇文章主要介绍了vue数组动态刷新失败问题及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-03-03

最新评论