Vue3 getCurrentInstance与ts结合使用的问题 | 您所在的位置:网站首页 › htmldivelement报错 › Vue3 getCurrentInstance与ts结合使用的问题 |
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 实验室设备网 版权所有 |