Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VS Code使用之Vue工程配置format代码格式化 #5

Open
way2ex opened this issue Dec 29, 2017 · 0 comments
Open

VS Code使用之Vue工程配置format代码格式化 #5

way2ex opened this issue Dec 29, 2017 · 0 comments
Labels

Comments

@way2ex
Copy link
Owner

way2ex commented Dec 29, 2017

编辑器另外一个很重要的功能就是代码格式化了,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文件中的 htmljavascript 代码都有格式化支持了。

使用符合 ESLint 规范的格式化

  1. 使用 prettier 格式化 .js 文件
    可以设置 prettier 在格式化的时候,读取项目的 .eslintrc的配置,对 js代码进行格式化。
  • 首先为 VS Code 安装 prettier 插件
  • 在 VS Code 的设置中添加如下配置
      "prettier.eslintIntegration": true
  1. .vue文件的格式化
    prettier插件对 .vue 文件格式化时,暂不能支持将 eslint规范应用到 js代码中。
    但是我们可以先将其格式化,然后利用 ESLint 保存文件时自动修复错误的特性,修复一些诸如 分号空格缩进 等错误。

参考资料:

@way2ex way2ex added the VS Code label Dec 29, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant