简体   繁体   English

vscode 快捷方式中的 (crl + /) 使 jsx 注释不正确

[英](crl + /) in vscode short cut makes incorrect jsx comment

In vscode in react class component (crl + /) will create the correct comment for JSX在 vscode 中反应 class 组件(crl + /)将为 JSX 创建正确的注释

{/* comment */}

But in vscode in react functional component (crl + /) will create an incorrect comment for JSX但是在 vscode 中的 react 功能组件 (crl + /) 会为 JSX 创建一个不正确的注释

// comment

How do you fix this in vscode你如何在vscode中解决这个问题

在此处输入图像描述

Based on the asker's comments, this is caused a the Babel extension.根据提问者的评论,这是由 Babel 扩展引起的。

You do not need any extensions to work with react in VS Code.您不需要任何扩展即可在 VS Code 中使用 react。 This Babel extension specifically was last updated in 2016 and can break many basic language features such as highlighting and commenting 这个 Babel 扩展特别是在 2016 年最后一次更新,可以打破许多基本的语言功能,例如突出显示和评论

I spent few hours on this problem, and the easiest solution I found is the following: Yes the problem is coming with the installation of Babel ES6/ES7 extension as many noticed, and when you uninstall or deactivate it, VScode retrieves it's normal behaviour: Ctrl + / => Toggle Line Comment for line or block selected with // for JS, JSX, ... files;我在这个问题上花了几个小时,我找到的最简单的解决方案如下:是的,正如许多人注意到的那样,安装 Babel ES6/ES7 扩展程序会出现问题,当您卸载或停用它时,VScode 会检索它的正常行为: Ctrl + / => 为用//为 JS、JSX、... 文件选择的行或块切换行注释; Shift + Alt + A => Toggle Block Comment for line or block selected between <!-- --> in HTML files, between /* */ in JS expressions and between {/* */} in JSX files for markup tags in render/return... So if you want to keep Babel ES6/ES7 extension active and still have such a behaviour: You can parameter your own shortcut key-binding combination in the file keybindings.json ( File/Preferences/Keyboard Shortcuts (or Ctrl+K+S) and then click the little file icon on top right for selecting Open Keyboard Shortcuts wich opens keybindings.json) where you use the VScode build-in command "editor.action.insertSnippet" as following: Shift + Alt + A => 为在 HTML 文件中的<!-- -->之间、JS 表达式中的/* * */ / 和 JSX 文件中的{/* */}之间的标记标签之间选择的行或块切换块注释渲染/返回...因此,如果您想保持 Babel ES6/ES7 扩展处于活动状态并且仍然具有这样的行为:您可以在文件 keybindings.json (文件/首选项/键盘快捷方式(或Ctrl+K+S)然后单击右上角的小文件图标以选择打开键盘快捷方式,即打开 keybindings.json)在其中使用 VScode 内置命令“editor.action.insertSnippet”,如下所示:

[
    {
    "key": "shift+alt+a",
    "command": "editor.action.insertSnippet",
    "args": {
        "snippet": "{/*\n ${TM_SELECTED_TEXT} \n*/}$0"
    },
    "when": "editorLangId == 'javascript' && editorTextFocus && !editorReadonly"
    }
]

Place the /n where you want in the expression for breaking lines, and the $0 for final placement(s) of cursor./n放置在表达式中用于断线的位置,将$0放置在 cursor 的最终放置位置。 Then save, and it's working straight:) only in JS and JSX files If you want to specify another language just replace 'javascript' in the "when" expression by the one you want from this VScode Language Identifiers list: https://code.visualstudio.com/docs/languages/identifiers#_known-language-identifiers And of course if you want another snippet behaviour: just replace the {/* by what you want in the "args" expression.然后保存,它可以直接工作:) 仅在 JS 和 JSX 文件中如果你想指定另一种语言,只需将"when"表达式中的'javascript'替换为此 VScode 语言标识符列表中所需的语言: https://code .visualstudio.com/docs/languages/identifiers#_known-language-identifiers当然,如果你想要另一个片段行为:只需在"args"表达式中用你想要的替换{/*

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

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