原有vue项目支持typescript
-
安装依赖
npm i vue-class-component vue-property-decorator --save
npm i ts-loader typescript tslint tslint-loader tslint-config-standard -D
初始化tsconfig
npx tsc --init
会自动生成 tsconfig.json (把"allowJs": true, 这个打开)允许文件中存在js,要不需要改太多东西
新增这些配置:
和compilerOptions 同级
"include": [ "src/**/*.ts", "src/**/*.tsx", "src/**/*.vue", "tests/**/*.ts", "tests/**/*.tsx" ], // ts 排除的文件 "exclude": ["node_modules"]
vue.config.js
configureWebpack: { resolve: { extensions: [".ts", ".tsx", ".js", ".json"] }, module: { rules: [ { test: /\.ts$/, exclude: /node_modules/, enforce: 'pre', loader: 'tslint-loader' }, { test: /\.tsx?$/, loader: 'ts-loader', exclude: /node_modules/, options: { appendTsSuffixTo: [/\.vue$/], } } ] } },
新建ts解析.vue 在src目录下添加俩文件
shims-tsx.d.ts
// shims-tsx.d.ts src目录下 import Vue, { VNode } from 'vue'; declare global { namespace JSX { // tslint:disable no-empty-interface interface Element extends VNode {} // tslint:disable no-empty-interface interface ElementClass extends Vue {} interface IntrinsicElements { [elem: string]: any; } } }
shims-vue.d.ts
// shims-vue.d.ts src目录下 declare module '*.vue' { import Vue from 'vue'; export default Vue; } //为了typescript做的适配定义文件,因为.vue文件不是一个常规的文件类型, //TypeScript是不能理解vue文件是干嘛的,加这一段是是告诉 TypeScript,vue文件是这种类型的。 //没有这个文件,会发现 import 导入的所有.vue类型的文件都会报错。 //axios报错 declare module 'axios' { interface AxiosInstance { (config: AxiosRequestConfig): Promise<any> } }
添加tslint.json
{ "extends": "tslint-config-standard", "globals": { "require": true } }
main.js改成main.ts 配置vue.config.js的入口为 main.ts
pages: { index: { entry: 'src/main.ts', } },
安装 @typescript-eslint/parser 将eslint校验改成 @typescript-eslint/parser
npm install @typescript-eslint/parser --save
更改.eslintrc.js
parserOptions: { parser: '@typescript-eslint/parser' },
-
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通