简体   繁体   English

在 Visual Studio 代码中禁用 onsave 格式化 for.ejs 文件

[英]Disable onsave formating for .ejs files in visual studio code

I'm working with ejs files, but in order to reuse some code, I'm using the includes feature.我正在使用 ejs 文件,但为了重用一些代码,我正在使用包含功能。 Given that some opening/closing html tags are placed in other files, after I save my changes, something (I don't know if prettier extension or VS code editor) is including the closing tags into my code, causing several problems when I running it.鉴于一些打开/关闭 html 标签放置在其他文件中,在我保存更改后,某些东西(我不知道是更漂亮的扩展还是 VS 代码编辑器)将关闭标签包含在我的代码中,导致我运行时出现一些问题它。

An other solution is to use a .prettierignore file and put in:另一种解决方案是使用.prettierignore文件并放入:

*.ejs

I don't use EJS myself, but after playing around with it in VSCode, I noticed that my .ejs files were being treated as HTML files.我自己不使用 EJS,但是在 VSCode 中使用它之后,我注意到我的.ejs文件被视为 HTML 文件。 You can see how your VSCode is interpreting the file by looking in the bottom right corner of the editor.您可以通过查看编辑器的右下角来查看 VSCode 是如何解释文件的。 You could search for EJS extensions as well.您也可以搜索 EJS 扩展。

You likely have the "Editor: format on save" option enabled.您可能启用了“编辑器:保存时的格式”选项。 To disable that setting, go to Preferences, and in the search bar type "format on save".要禁用该设置,请将 go 设置为首选项,然后在搜索栏中键入“保存时格式化”。 Find the setting.找到设置。 Uncheck the box.取消选中该框。 That should fix the problem.那应该可以解决问题。

If, as you mentioned, your closing HTML tags are in other files, then you have invalid HTML and the formatter (both Prettier's and the default one) will autocomplete the closing tag.如您所述,如果您的结束 HTML 标记位于其他文件中,则您的 HTML 无效,并且格式化程序(Prettier 的和默认的)将自动完成结束标记。 (I would wager EJS also wouldn't like the lack of closing tags, but since I don't use it I'm not so sure, maybe it's fine.) (我打赌 EJS 也不喜欢缺少结束标签,但由于我不使用它,我不太确定,也许没关系。)

在此处输入图像描述

in your settings.json you should add these settings.在您的settings.json ,您应该添加这些设置。 just instead of "[css]" type what you want.只是输入你想要的而不是"[css]" (the format of the file) (文件的格式)

and just please ignore line 2:) that's not related to this answer.请忽略与此答案无关的第 2 行 :)。

Edit编辑

as @aegatlin said use this: "[html]" .(if "[ejs]" didn't work for you)正如@aegatlin 所说,使用这个: "[html]" 。(如果"[ejs]"对你不起作用)

Zulhilmi Zainudin has the solution https://medium.com/@zulhhandyplast/how-to-disable-vs-code-formatonsave-for-specific-file-extensions-c60e8f254243 Zulhilmi Zainudin 有解决方案https://medium.com/@zulhhandyplast/how-to-disable-vs-code-formatonsave-for-specific-file-extensions-c60e8f254243

In vscode setting file, associate ejs extentions files to a « language ».在 vscode 设置文件中,将 ejs 扩展文件与“语言”相关联。 Then you can specify different rules for this that language:然后,您可以为此语言指定不同的规则:

.vscode/settings.json file content: .vscode/settings.json文件内容:

{
    "files.associations": {
        "*.ejs": "ejs" // this create the language « ejs » which refers to any .ejs file
    },
    "[ejs]": { // custom settings for the language « ejs »
        "editor.formatOnSave": false
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM