在我安装并启用了ESlint和Prettier的Nuxt应用程序中,我切换到Visual Studio Code。

当我打开一个.vue文件并按CMD+ Shift + P并选择格式化文档时,我的文件根本没有被格式化。

我的.prettierrc设置:

{
  "tabWidth": 2,
  "semi": false,
  "singleQuote": true
}

我有这么多源代码行,所以我不能手动格式化它们。我做错了什么?


当前回答

对我来说,当我在. preterrc文件中去掉parser: json时,它起作用了。

其他回答

你只需要配置你的默认格式化器,并勾选设置中的Format On Save复选框,在安装prettier后,使其工作。不要乱动其他配置文件。

1 -选择Default Formatter

打开文件->首选项->设置(或Windows中的Ctrl +)。 搜索编辑器:默认格式化器 选择您的默认格式化程序为pretty - Code formatter;

见下图;

2 -保存格式

打开文件->首选项->设置(或Windows中的Ctrl +)。 搜索编辑器:保存格式 单击Format On Save下的复选框;

见下图;

这里我的漂亮配置工作在vue.js文件,typescript文件和json文件。

arrowParens: 'always'
bracketSpacing: true
endOfLine: 'crlf'
htmlWhitespaceSensitivity: 'css'
insertPragma: false
jsxBracketSameLine: false
jsxSingleQuote: true
overrides:
- files: '*.json'
  options:
    semi: true
    parser: 'json'
parser: 'babel'
printWidth: 120
proseWrap: 'preserve'
quoteProps: 'truepreserve'
requirePragma: false
semi: false
singleQuote: true
tabWidth: 8
trailingComma: 'es5'
useTabs: true
vueIndentScriptAndStyle: 

不要忘记更新你的vscode设置

{
"extensions.ignoreRecommendations": false,
"editor.suggestSelection": "first",
"vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue",
"[json]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[javascript]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[vue]": {
    "editor.defaultFormatter": "octref.vetur"
},
"[typescript]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[html]": {
    "editor.defaultFormatter": "vscode.html-language-features"
},
"vetur.format.options.useTabs": true}

那是所有人的事!

检查是否有.vscode/settings。Json文件在您的项目目录(工作空间)。在我的案例中,有人签入了这个文件:

{
  "editor.formatOnSave": false
}

解决方案:删除文件(也从源代码控制中删除),并将.vscode/添加到.gitignore(如果您使用git)。

这就是我的工作(我的默认格式化器已经设置为更漂亮)

将默认格式化程序更改为default 重启vscode 将默认格式化程序更改为pretty。

在我的案例中,我必须做到以下几点:

从命令行安装prettier (npm Install——save-dev——save-exact prettier-plugin-custom) 重载VSCode

voilà,一切都开始运转了。

提示: 为了确保安装良好,我检查了版本:

npx prettier --version