最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Vue中使用eslint和editorconfig方式代码示例
时间:2022-06-29 01:46:27 编辑:袖梨 来源:一聚教程网
本篇文章小编给大家分享一下Vue中使用eslint和editorconfig方式代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
使用eslint的好处
1、避免运行时因格式问题报错
2、方便团队合作,代码风格统一
安装eslint
命令行执行:
npm i eslint eslint-config-standard eslint-plugin-standard eslint-plugin-promise eslint-plugin-import eslint-plugin-node eslint-plugin-html -D
eslint-plugin-html插件识别html文件中的script标签里面的JavaScript
文件配置说明
在项目目录新建.eslintrc文件:
{
"extends": "standard",
"plugins": [
"html"
],
"rules": {
"no-new": "off"
}
}
启动命令配置
在package.json的scripts中加入:
"lint": "eslint --ext .js --ext .jsx --ext .vue client/", "lint-fix": "eslint --fix --ext .js --ext .jsx --ext .vue client/"
client/ 为要检查的文件夹
执行:
npm run lint //语法检查 npm run lint-fix //自动修复语法检查出现的问题
自动检查语法配置
命令行执行:
npm i eslint-loader babel-eslint -D
然后在.eslintrc文件中添加"parser": "babel-eslint":
{
"extends": "standard",
"plugins": [
"html"
],
"parser": "babel-eslint",
"rules": {
"no-new": "off"
}
}
在webpack的配置文件的module.rules中加入:
{
test: /.(vue|js|jsx)$/,
loader: 'eslint-loader',
exclude: /node_modules/,
enforce: 'pre' //预处理
},
添加editorconfig
在项目目录新建.editorconfig文件:
root = true
[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
vue editorconfig编辑器配置说明
editorconfig是什么,有什么用
editorconfig是定义编码样式的配置文件,帮助多人合作项目或者不同编辑器下保持代码风格统一。
editorconfig示例
# http://ed*it*o*rconfig.org (Editorconfig 的官方网站) # 控制 EditorConfig 约定的范围 root = true # 匹配全部文件 [*] # 设置字符集 charset = utf-8 # 缩进风格 可选"space"、"tab" indent_style = space # 缩进大小 可以是数字(空格数), tab(如果tab大小没设置则按编辑器默认tab大小) indent_size = 2 # tab的宽度 tab_# 结尾换行符,可选"lf"、"cr"、"crlf" end_of_line = lf # 文件最后插入一个空行 insert_final_newline = true # 删除行尾空格 trim_trailing_whitespace = true # 匹配.java结尾的文件 [*.java] # 匹配.md结尾的文件 [*.md] trim_trailing_whitespace = false
root=true 控制 EditorConfig 约定的范围 , Visual Studio 会在打开的文件的目录和每个父目录中查找名为 .editorconfig 的文件。 到达根文件路径时或找到具有 root=true 的 .editorconfig 文件时搜索结束。
检查是否生效
在项目的 js 文件中使用 tab 键进行缩进,分别修改 indent_size 属性值为 2 和 4,观察是否有变化。
如果没有任何变化,说明还没有安装 Editorconfig 插件。
Editorconfig 插件
该插件的作用是告诉开发工具,如 Webstorm 自动去读取项目根目录下的 .editorconfig 配置文件,如果没有安装这个插件,光有一个配置文件是无法生效的。
Webstorm 2017.1 版本之后都是自动安装这个插件的。
相关文章
- 百炼英雄怎么挂机 挂机方式及各等级最优点位攻略 12-15
- 百炼英雄vip礼包码大全 最新可用vip礼包码 12-15
- 光与影33号远征队符文收集:强力复生符文效果及获取方式详解 12-15
- 百炼英雄兑换码有哪些 最新可用兑换码及兑换方式 12-15
- 神器传说怎么克制闪避流 闪避流克制阵容实战攻略 12-15
- 神器传说怎么打异兽分最高 异兽冲榜高分搭配 12-15