Tn_i
Tn_i
  • 发布:2022-01-12 15:15
  • 更新:2022-01-12 15:15
  • 阅读:1450

vue3 ts cli创建的项目 为 globalProperties 扩充类型 没有效果

分类:uni-app

问题:
跟着vue3官方文档步骤进行 扩充类型但无效果。

环境:

  1. vscode
  2. typescript 版本:^4.5.4
  3. @dcloudio/types 版本:^2.5.16

尝试:

  1. 在 main.ts 文件内添加类型声明


    会提示 ”扩大中的模块名无效,找不到模块“@vue/runtime-core”。“

  2. 在 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 的类型

2022-01-12 15:15 负责人:无 分享
已邀请:

该问题目前已经被锁定, 无法添加新回复