Vue3 getCurrentInstance与ts结合使用的问题 您所在的位置:网站首页 htmldivelement报错 Vue3 getCurrentInstance与ts结合使用的问题

Vue3 getCurrentInstance与ts结合使用的问题

2023-09-27 08:45| 来源: 网络整理| 查看: 265

vue3项目中,如果不用ts这样使用是没问题的

const { proxy } = getCurrentInstance()

在ts中使用会报错:报错:...类型“ComponentInternalInstance | null”

我们在项目中一般会用到很多getCurrentInstance()方法,直接封装一下

创建useCurrentInstance.ts文件:

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

组件内使用:

import { defineComponent } from "vue"; import useCurrentInstance from "@/utils/useCurrentInstance"; export default defineComponent({ setup() { const { proxy } = useCurrentInstance(); console.log(proxy); }, });


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有