vue3+ts向所有组件实例注入自定义属性之后声明 @vue/runtime-core 模块会导致 vue 方法导出错误
注入自定义属性如下
app.config.globalProperties.api = api; // 向所有组件注入 api 对象
注入之后是不能在组件中使用的,因为 ts 类型的原因,组件实例是没有 api 的声明的,所以需要额外声明
声明
// shims-vue.d.ts 中追加内容
declare module "@vue/runtime-core" {
import type { ApiConfig } from "@/api";
interface ComponentCustomProperties {
api: ApiConfig;
}
}
报错
添加如上声明之后所有对 vue 的导入语句全部报错
import { inject, ref, reactive, getCurrentInstance, onMounted } from "vue";
报错信息类似如下:TS2305: Module ‘“../../node_modules/vue/dist/vue”‘ has no exported member ‘getCurrentInstance’.
推荐文章: