简体   繁体   English

如何启用VS Code HTML错误检查/验证?

[英]How to enable VS Code HTML error checking / validation?

I have just switched to Visual Studio Code for web development from Netbeans and am finding my way around. 我刚刚从Netbeans切换到Visual Studio Code进行Web开发,并且正在寻找解决方法。 In Netbeans, if I forget the closing bracket on a tagname it will indicate my error with a red squiggly underline, and the alert in the left margin. 在Netbeans中,如果我忘记了标记名的右括号,它将用红色的波浪形下划线指示警告我的错误,并在左边距显示警告。

I would have thought error checking to be a fundamental function in a web dev editor like vs code. 我本来认为错误检查是Web开发编辑器(如vs代码)中的一项基本功能。 Maybe I am just not finding the right option or extension. 也许我只是找不到合适的选项或扩展名。

How can I achieve this same html, css error checking behaviour in VS Code? 如何在VS Code中实现相同的html,css错误检查行为?

VSCode doesn't have HTML validation by default. VSCode默认没有HTML验证。 But it allows you to add extensions and enable these features. 但是,它允许您添加扩展并启用这些功能。

To add HTML validation (linting), Open VSCode, then press Ctrl + P and then paste ext install HTMLHint in it, and press enter. 要添加HTML验证(启动),请打开VSCode,然后按Ctrl + P ,然后在其中粘贴ext install HTMLHint ,然后按Enter。 It will install a HTML validator. 它将安装HTML验证程序。 You may need to reload VSCode to load the extension. 您可能需要重新加载VSCode才能加载扩展。

Now if you open the same html document you had the syntax error, you should see there's an issue shown at the status bar at the bottom :) and it will also show you the errors in those lines. 现在,如果打开相同的html文档,则出现语法错误,您应该会看到状态栏底部显示一个问题:),并且还会在这些行中显示错误。

VS Code default support code formatting and it track the syntactical error. VS Code默认支持代码格式化,它可以跟踪语法错误。 If you create a new file and directly try to write the code than VS Code could not able to understand which language or type of syntax user want to format/correcting. 如果您创建一个新文件并直接尝试编写代码,则VS Code无法理解用户要格式化/更正的语言或语法类型。 So, first need to save the new file with the proper extension than visual studio properly identify the syntax. 因此,首先需要以适当的扩展名保存新文件,而不是Visual Studio正确识别语法。

The code formatting is available in VS Code through the following shortcuts: 可通过以下快捷方式在VS Code中使用代码格式:

  • On Windows Shift + Alt + F 在Windows上Shift + Alt + F
  • On Mac Shift + Option + F 在Mac上Shift + Option + F
  • On Ubuntu Ctrl + Shift + I 在Ubuntu上Ctrl + Shift + I

You can add Auto Close Tag from VS Code marketplace. 您可以从VS Code市场添加Auto Close Tag

Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter. 启动VS Code快速打开(Ctrl + P),粘贴以下命令,然后按Enter。

  1. Automatically add HTML/XML close tag, same as Visual Studio IDE or Sublime Text 自动添加HTML / XML关闭标签,与Visual Studio IDE或Sublime Text相同

     ext install auto-close-tag 
  2. VS Code integration for HTMLHint - A Static Code Analysis Tool for HTML 用于HTMLHint的VS代码集成-用于HTML的静态代码分析工具

     ext install HTMLHint 
  3. Provides CSS class name completion for the HTML class attribute based on the CSS files in your workspace. 根据工作空间中的CSS文件为HTML类属性提供CSS类名称完成。 Also supports React's className attribute. 还支持React的className属性。

     ext install html-css-class-completion 

The VS Code supports this by default. VS Code默认支持此功能。 It also has Emmet support enabled by default which means you don't need to worry about close HTML tags. 它还默认启用了Emmet支持,这意味着您不必担心关闭HTML标记。 It automatically adds closing tag. 它会自动添加结束标签。

This is not a builtin functionality of VsCode...However, it has a lot of plugins available. 这不是VsCode的内置功能...但是,它有很多可用的插件。 I would recommend you the HTMLHint plugin. 我建议您使用HTMLHint插件。 That's what I have been using. 这就是我一直在使用的。

You can install it using: ext install HTMLHint command 您可以使用以下命令进行ext install HTMLHintext install HTMLHint命令

There is an extension for VS Code which is HTMLHint. VS Code的扩展名为HTMLHint。
You can install that by following the below 您可以按照以下说明进行安装
1. To open the extensions:marketplace press Ctrl + Shift + X 1.要打开扩展程序:市场,请按Ctrl + Shift + X
2. Type HTMLHint in the search extension box 2.在搜索扩展框中输入HTMLHint
3. Click on the install button in the search result showing HTMLHint developed by "Mike Kaufman" 3.单击搜索结果中的“安装”按钮,显示“ Mike Kaufman”开发的HTMLHint

I found that this extension (while not letting you extend the built in rules) works better than HTMLHint for checking HTML validity. 我发现此扩展名(虽然不允许您扩展内置规则)在检查HTML有效性方面比HTMLHint更好。

Name: W3C Validation 名称:W3C验证

Id: umoxfo.vscode-w3cvalidation ID:umoxfo.vscode-w3cvalidation

Description: Adds W3C validation support to Visual Studio Code. 说明:将W3C验证支持添加到Visual Studio Code。

Version: 2.3.0 版本:2.3.0

Publisher: Umoxfo VS 发布者:Umoxfo VS

Marketplace Link: https://marketplace.visualstudio.com/items?itemName=umoxfo.vscode-w3cvalidation 市场链接: https : //marketplace.visualstudio.com/items?itemName=umoxfo.vscode-w3cvalidation

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

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