1、初始化
npm init vue@latest
2、tsconfig.json配置
{
"compilerOptions": {
"sourceMap": false, // 生成目标文件的sourseMap文件
"strict": true, // 启用严格模式
"noImplicitAny": false, // 不允许隐式的any类型
"module": "esnext", // 指定生成代码的模板标准
"target": "es6", // 目标语言的版本
"moduleResolution": "Node", // 模块解析策略,ts默认使用node的解析策略,即相对的方式导入
"removeComments": true, // 移除注释
"strictNullChecks": true, // 不允许把null,undefined赋值给其它类型的变量
"resolveJsonModule": true, // 允许导入json文件
"baseUrl": ".", // 解析非相对模块的基地址,默认是当前目录,防止引入文件报错
"diagnostics": true, // 打印诊断信息
"paths": { // 路径映射
"/@/*": ["src/*"],
"/server/*": ["server/*"]
},
"lib": ["esnext", "dom"], // TS需要引用的库,即声明文件,es5 默认引用dom、es5、scripthost,如需要使用es的高级版本特性,通常都需要配置,如es8的数组新特性需要引入"ES2019.Array",
"types": ["vite/client", "jest", "node"], // 加载的文件声明包
"jsx":"preserve", // jsx解析模板
"esModuleInterop": true, // 允许导入而外的ts支持
"skipLibCheck":true, // 所有声明文件(*.d.ts)的类型检查,解决打包不报错
"suppressImplicitAnyIndexErrors":true, // 允许字符串下标表达式
"plugins": [
{ "name": "@vuedx/typescript-plugin-vue" }
]
},
"include": [ // 指定一个匹配列表(属于自动指定该路径下的所以ts相关文件)
"**/*.ts", "**/*.d.ts", "**/*.vue"
],
"exclude": [
"node_modules"
]
}
3、eslint格式化校验 + prettier自动格式化
a、prettier常用配置:
{
"printWidth": 120, // 超过最大值换行
"tabWidth": 2, // 缩进字节数
"useTabs": false, // 缩进不使用tab,使用空格
"semi": true, // 句尾添加分号
"singleQuote": true, // 使用单引号代替双引号
"proseWrap": "preserve", // 默认值。因为使用了一些折行敏感型的渲染器(如GitHub comment)而按照markdown文本样式进行折行
"arrowParens": "avoid", // (x) => {} 箭头函数参数只有一个时是否要有小括号。avoid:省略括号
"bracketSpacing": true, // 在对象,数组括号与文字之间加空格 "{ foo: bar }"
"disableLanguages": ["vue"], // 不格式化vue文件,vue文件的格式化单独设置
"endOfLine": "auto", // 结尾是 \n \r \n\r auto
"eslintIntegration": false, //不让prettier使用eslint的代码格式进行校验
"htmlWhitespaceSensitivity": "ignore",
"ignorePath": ".prettierignore", // 不使用prettier格式化的文件填写在项目的.prettierignore文件中
"jsxBracketSameLine": false, // 在jsx中把'>' 是否单独放一行
"jsxSingleQuote": false, // 在jsx中使用单引号代替双引号
"parser": "babylon", // 格式化的解析器,默认是babylon
"requireConfig": false, // Require a 'prettierconfig' to format prettier
"stylelintIntegration": false, //不让prettier使用stylelint的代码格式进行校验
"trailingComma": "es5", // 在对象或数组最后一个元素后面是否加逗号(在ES5中加尾逗号)
"tslintIntegration": false // 不让prettier使用tslint的代码格式进行校验
}
b、vscode安装Prettier - Code formatter插件
c、vscode设置中搜索format on save,勾上
d、文件预览界面,右键,使用。。。格式化文档,选择prettier