问题:
跟着vue3官方文档步骤进行 扩充类型但无效果。
环境:
- vscode
- typescript 版本:^4.5.4
- @dcloudio/types 版本:^2.5.16
尝试:
-
在 main.ts 文件内添加类型声明
会提示 ”扩大中的模块名无效,找不到模块“@vue/runtime-core”。“
-
在 src 创建一个 type.d.ts 文件
// type.d.ts 文件 import request from './apis/request'; import * as config from './apis/config'; declare module '@vue/runtime-core' { export interface ComponentCustomProperties { $http: typeof request; $cfg: typeof config; } }
// vue 文件
<script lang="ts">
export default defineComponent({
...
methods: {
doSomeThing() {
// 在这里尝试写 this.$http 发现提示不存在属性 $http
}
}
...
})
</script>
没有红线提示找不到模块了,但是在 vue 文件里 依旧识别不出来 this.$http 和 this.$cfg 的类型