问题原因
之前我们在更改js文件为ts文件会发现之前引入的vue文件会报错,这是因为TypeScript只能理解.ts文件不能理解.vue文件
解决办法
1.在网上搜索vue3 can not find modules,有一个解决方案,建一个shims-vue.d.ts,必须以.d.ts结尾的文件
declare module '*.vue' {
import { ComponentOptions } from 'vue'
const component: ComponentOptions
export default component
}
但是这种方式在我这边并没有运行成功,所以有了下面的方法二
- TypeScript 配置
2.1 全局安装 typescript:
yarn global add typescript
2.2 在项目根目录运行:tsc --init就可以得到 tsconfig.json
2.3 把 tsconfig.json 的内容改为如下内容:
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"strict": false,
"jsx": "preserve",
"moduleResolution": "node"
}
}
这样就配置好了 TypeScript。