Vue-cli Eslint在vscode里代码自动格式化的方法

本篇文章主要介绍了Vue-cli Eslint在vscode里代码自动格式化的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

编辑器另外一个很重要的功能就是代码格式化了,VS Code默认提供常见文件的格式化,如.js,.html等。

添加对.vue文件的格式化支持

这里我们添加对 .vue 文件的格式化支持。

1. 安装 Vetur 插件
2. 在 VS Code 的设置中添加如下规则:

 { "vetur.format.defaultFormatter": { "html": "prettier", "css": "prettier", "postcss": "prettier", "scss": "prettier", "less": "prettier", "js": "prettier", "ts": "prettier", "stylus": "stylus-supremacy" } }

这里是设置 Vetur 默认使用的格式化插件 prettier。 这样对于.vue文件中的 html 和 javascript 代码都有格式化支持了。

vue eslint 代码自动格式化

  1. vue-cli 代码风格为JavaScript Standard Style 代码检查规范严格,一不小心就无法运行,使用eslint的autoFixOnSave可以在保存代码的时候自动格式化代码
  2. VSCode拓展插件推荐(HTML、Node、Vue、React开发均适用)

建议阅读:

  1. vscode插件推荐
  2. JavaScript Standard Style 代码风格规范

正文 eslint 自动格式化

配置环境:

 npm i -g eslint-plugin-vue #or npm i -S eslint-plugin-vue

创建项目跟路径下的文件:.eslintrc | .eslint.js

 // 添加插件 "plugins": [ "vue" ]

在你的vscode设置文件里添加:

1、在vscode添加 eslint 插件
2、在vscode添加 vetur 插件
3、修改你的setting.json

 // 添加进你的vscode的 setting.json "eslint.autoFixOnSave": true, "eslint.validate": [ "javascript",{ "language": "vue", "autoFix": true },"html", "vue" ], 

参考文集:

https://stackoverflow.com/questions/45852531/eslint-code-format-in-vue
https://github.com/varHarrie/Dawn-Blossoms/issues/10

以上就是Vue-cli Eslint在vscode里代码自动格式化的方法的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Vue.js 教程