Vue3 Ts Setup 中如何正确使用 getCurrentInstance 遇到问题及解决方法?

2026-04-01 13:161阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计734个文字,预计阅读时间需要3分钟。

Vue3 Ts Setup 中如何正确使用 getCurrentInstance 遇到问题及解决方法?

目录+环境+问题描述+问题解决+补充:Vue3 + getCurrentInstance 与 ts 结合使用的坑

一、关于在 ts 中使用 getCurrentInstance 的类型定义错误问题

二、不能使用 getCurrentInstance 的 ctx

总结+环境:vscode + typescript 4 + vue3+ 问题描述

目录
  • 环境
  • 问题描述
  • 问题解决
  • 补充:Vue3 getCurrentInstance与ts结合使用的坑
    • 一、关于在ts中使用到类型定义错误问题
    • 二.不能使用getCurrentInstance的ctx
  • 总结

    环境

    vscode
    typescript4
    vue3

    问题描述

    首先,vue3中的全局变量及方法的配置较vue2中的变化大家应该已经知道了,不清楚的可以查看官方说明,但是按照官方文档结合typescript使用时遇到了问题:

    axios.ts

    // axios.ts import axios from 'axios' const app = Vue.createApp({}) // 全局自定义属性 declare module '@vue/runtime-core' { interface ComponentCustomProperties { $axios: AxiosInstance; } } app.config.globalProperties.$axios = axios

    任意.vue文件

    <script lang="ts" setup> import { getCurrentInstance } from 'vue'; // 首先 此处 proxy ts会报 // 类型“ComponentInternalInstance | null”上不存在属性“proxy”。ts(2339) const { proxy } = getCurrentInstance() // 然后下面会报这个错误 // Unsafe member access .$axios on an `any` value. eslint@typescript-eslint/no-unsafe-member-access // Unsafe call of an `any` typed value. eslint@typescript-eslint/no-unsafe-call proxy.$axios('')

    以上就是报错的全部内容,接下来我们解决这个问题

    问题解决

    • 第一个报错很好理解 因为getCurrentInstance()的返回类型存在null所以在此处添加断言即可

    import { ComponentInternalInstance, getCurrentInstance } from 'vue'; // 添加断言 const { proxy } = getCurrentInstance() as ComponentInternalInstance

    2.但是改完后我们发现下面依旧会有报错

    Vue3 Ts Setup 中如何正确使用 getCurrentInstance 遇到问题及解决方法?

    // 对象可能为 "null"。ts(2531) proxy.$axios('')

    这个解决起来更简单了,在proxy后面添加?来过滤null的结果

    proxy?.$axios('')

    以上,问题解决!

    补充:Vue3 getCurrentInstance与ts结合使用的坑

    一、关于在ts中使用到类型定义错误问题

    报错:...类型“ComponentInternalInstance | null”

    就嗝屁了。。。

    1. 可以添加ts忽略去解决

    // @ts-ignoreconst { proxy } = getCurrentInstance();

    但是这个方法很无脑,也麻烦。。。

    2. 考虑到在获取上下文和全局挂载实例的时候会用到这个getCurrentInstance,那我们来新建 hooks\useCurrentInstance.ts

    import { ComponentInternalInstance, getCurrentInstance } from 'vue'export defaultfunction useCurrentInstance() {     const { appContext } = getCurrentInstance() as ComponentInternalInstance     const globalProperties = appContext.config.globalProperties     return {         globalProperties     } }

    组件中使用

    // 先引入文件 import useCurrentInstance from "@/hooks/useCurrentInstance"; ...// 在setup 中使用处理 const { globalProperties } = useCurrentInstance();

    二.不能使用getCurrentInstance的ctx

    我们在获取Vue3中全局挂载的方法时会这么写:

    这里的ctx不是setup提供的ctx

    const { ctx } = getCurrentInstance()

    这里ctx打包后在生产环境下是获取不到的,请各位没玩过生产的别不小心误导到别人啊哈,恰好在Vue3的issues中找到的。

    正确应该使用

    const { proxy } = getCurrentInstance()

    总结

    到此这篇关于Vue3+ts+setup getCurrentInstance使用时遇到问题以及解决的文章就介绍到这了,更多相关Vue3 ts setup getCurrentInstance使用问题内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

    本文共计734个文字,预计阅读时间需要3分钟。

    Vue3 Ts Setup 中如何正确使用 getCurrentInstance 遇到问题及解决方法?

    目录+环境+问题描述+问题解决+补充:Vue3 + getCurrentInstance 与 ts 结合使用的坑

    一、关于在 ts 中使用 getCurrentInstance 的类型定义错误问题

    二、不能使用 getCurrentInstance 的 ctx

    总结+环境:vscode + typescript 4 + vue3+ 问题描述

    目录
    • 环境
    • 问题描述
    • 问题解决
    • 补充:Vue3 getCurrentInstance与ts结合使用的坑
      • 一、关于在ts中使用到类型定义错误问题
      • 二.不能使用getCurrentInstance的ctx
    • 总结

      环境

      vscode
      typescript4
      vue3

      问题描述

      首先,vue3中的全局变量及方法的配置较vue2中的变化大家应该已经知道了,不清楚的可以查看官方说明,但是按照官方文档结合typescript使用时遇到了问题:

      axios.ts

      // axios.ts import axios from 'axios' const app = Vue.createApp({}) // 全局自定义属性 declare module '@vue/runtime-core' { interface ComponentCustomProperties { $axios: AxiosInstance; } } app.config.globalProperties.$axios = axios

      任意.vue文件

      <script lang="ts" setup> import { getCurrentInstance } from 'vue'; // 首先 此处 proxy ts会报 // 类型“ComponentInternalInstance | null”上不存在属性“proxy”。ts(2339) const { proxy } = getCurrentInstance() // 然后下面会报这个错误 // Unsafe member access .$axios on an `any` value. eslint@typescript-eslint/no-unsafe-member-access // Unsafe call of an `any` typed value. eslint@typescript-eslint/no-unsafe-call proxy.$axios('')

      以上就是报错的全部内容,接下来我们解决这个问题

      问题解决

      • 第一个报错很好理解 因为getCurrentInstance()的返回类型存在null所以在此处添加断言即可

      import { ComponentInternalInstance, getCurrentInstance } from 'vue'; // 添加断言 const { proxy } = getCurrentInstance() as ComponentInternalInstance

      2.但是改完后我们发现下面依旧会有报错

      Vue3 Ts Setup 中如何正确使用 getCurrentInstance 遇到问题及解决方法?

      // 对象可能为 "null"。ts(2531) proxy.$axios('')

      这个解决起来更简单了,在proxy后面添加?来过滤null的结果

      proxy?.$axios('')

      以上,问题解决!

      补充:Vue3 getCurrentInstance与ts结合使用的坑

      一、关于在ts中使用到类型定义错误问题

      报错:...类型“ComponentInternalInstance | null”

      就嗝屁了。。。

      1. 可以添加ts忽略去解决

      // @ts-ignoreconst { proxy } = getCurrentInstance();

      但是这个方法很无脑,也麻烦。。。

      2. 考虑到在获取上下文和全局挂载实例的时候会用到这个getCurrentInstance,那我们来新建 hooks\useCurrentInstance.ts

      import { ComponentInternalInstance, getCurrentInstance } from 'vue'export defaultfunction useCurrentInstance() {     const { appContext } = getCurrentInstance() as ComponentInternalInstance     const globalProperties = appContext.config.globalProperties     return {         globalProperties     } }

      组件中使用

      // 先引入文件 import useCurrentInstance from "@/hooks/useCurrentInstance"; ...// 在setup 中使用处理 const { globalProperties } = useCurrentInstance();

      二.不能使用getCurrentInstance的ctx

      我们在获取Vue3中全局挂载的方法时会这么写:

      这里的ctx不是setup提供的ctx

      const { ctx } = getCurrentInstance()

      这里ctx打包后在生产环境下是获取不到的,请各位没玩过生产的别不小心误导到别人啊哈,恰好在Vue3的issues中找到的。

      正确应该使用

      const { proxy } = getCurrentInstance()

      总结

      到此这篇关于Vue3+ts+setup getCurrentInstance使用时遇到问题以及解决的文章就介绍到这了,更多相关Vue3 ts setup getCurrentInstance使用问题内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!