Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set vscode format Vue template automatically on save?

I've modified the settings.json file, but it doesn't work.

Here it is:

{
    "eslint.autoFixOnSave": true,
    "vetur.format.defaultFormatter.html":"js-beautify-html"
}

like image 620
gwtjs Avatar asked Aug 21 '19 15:08

gwtjs


2 Answers

In your settings.json you should have:

  • editor.formatOnSave: true
  • [vue]: {"editor.defaultFormatter": "octref.vetur"} if you have several formatters registered for .vue files you need to specify which one to use (otherwise format on save will not know which one to use and it will default to do nothing. This will select "Vetur" as the default.
  • "vetur.format.defaultFormatter.html": "js-beautify-html" to tell Vetur how to format the part inside <template> tags:
{
    "editor.formatOnSave": true,
    "vetur.format.defaultFormatter.html": "js-beautify-html",
    "[vue]": {
        "editor.defaultFormatter": "octref.vetur"
    }  
}

Note: How do you know that there are several formatters registered for .vue? If when you use the Format Document action you get the dialog There are multiple formatters for 'Vue' files. Select a default formatter to continue then it means that you have more that one formatter registed for '.vue' files.

like image 167
RubenLaguna Avatar answered Oct 13 '22 18:10

RubenLaguna


  1. use plugin:vue/recommended replace plugin:vue/essential
// .eslintrc.js
module.exports = {
  extends: [
    'plugin:vue/recommended',
    '@vue/standard'
  ]
}
  1. enable eslint fix on save
// .vscode/settings.json
{
  "eslint.validate": [
    "javascript",
    "html",
    "vue"
  ],
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": true,
    "source.fixAll.stylelint": true
  },
  "vetur.validation.template": false
}
like image 39
F-loat Avatar answered Oct 13 '22 20:10

F-loat