这篇文章主要介绍“Vue中怎么使用eslint和editorconfig”,在日常操作中,相信很多人在Vue中怎么使用eslint和editorconfig问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Vue中怎么使用eslint和editorconfig”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
1、避免运行时因格式问题报错
2、方便团队合作,代码风格统一
命令行执行:
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文件:
root = true
[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
editorconfig是定义编码样式的配置文件,帮助多人合作项目或者不同编辑器下保持代码风格统一。
# http://editorconfig.org (Editorconfig 的官方网站)
# 控制 EditorConfig 约定的范围
root = true
# 匹配全部文件
[*]
# 设置字符集
charset = utf-8
# 缩进风格 可选"space"、"tab"
indent_style = space
# 缩进大小 可以是数字(空格数), tab(如果tab大小没设置则按编辑器默认tab大小)
indent_size = 2
# tab的宽度
tab_width = 4
# 结尾换行符,可选"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 插件。
该插件的作用是告诉开发工具,如 Webstorm 自动去读取项目根目录下的 .editorconfig 配置文件,如果没有安装这个插件,光有一个配置文件是无法生效的。
Webstorm 2017.1 版本之后都是自动安装这个插件的。
到此,关于“Vue中怎么使用eslint和editorconfig”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。