【Vscode】Ts文件引入vue文件报错问题处理

1,272 阅读1分钟

背景

vue文件在路由文件里面使用import文件的时候存在报错问题, 该问题应该出现在老版本的Vue-offical插件,请确认是否更新。

image.png

报错信息

Cannot find module '/xxx/xxx.vue' or its corresponding type declarations.ts(2307)

解决方案一

增加vue的声明文件, env.d.ts, 让ts文件能够识别vue文件后缀的路径,文件内容:

/// <reference types="vite/client" />

declare module '*.vue' {
    import type { DefineComponent } from 'vue';

    const vueComponent: DefineComponent<{}, {}, any>;

    export default vueComponent;
}

这样的话路径就不会报错,但是依旧会存在另外一个问题,当点击路径的时候不会跳转到相应的vue文件

image.png

这个问题暂时没有找到其他解决方法,推荐使用下面方案二

解决方案二

方案二就比较简单了,想使用Vscode快捷键 ctr/command + shift + p ,然后搜索Show build-in,然后在左边的内置的插件里面搜索typescript, 选择禁用工作区就可以解决了

image.png

暂时没看到该方案引起编辑器的其他问题。

解决方案三

更新Vue-Official, 后续出现这个弹窗,发现更新一下这个问题解决了,来自vscode的更新解决问题

image.png