简体   繁体   English

如何在 Visual Studio 代码 javascript 片段中创建自定义函数片段?

[英]How to create a custom function snippet in visual studio code javascript snippets?

I want to create a custom function snippet like this :我想创建一个像这样的自定义函数片段:

const handle = () => {

}

But when I put it in visual studio code javascript snippets JSON file it gets red underline which means the format is not correct :但是当我把它放在 Visual Studio 代码 javascript 片段 JSON 文件中时,它会变成红色下划线,这意味着格式不正确:

javascript.json

How can I put a custom function snippet with no error in visual studio code user snippets ?如何在 Visual Studio 代码用户片段中放置一个没有错误的自定义函数片段?

JSON body is an array so that you can provided multiple lines (array items) comma separated. JSON body是一个数组,因此您可以提供逗号分隔的多行(数组项)。

"React Function": {
  "prefix" : "f",
  "body": [
     "const handle = () => {",
     "$0",
     "}"
  ],
  "description": "some description"
}

You can't have newlines in JSON strings. JSON 字符串中不能有换行符。 Instead use \\n (or \\r\\n on windows).而是使用\\n (或在 Windows 上使用\\n \\r\\n )。 But VS code expects you to pass different lines as different strings.但是 VS 代码希望您将不同的行作为不同的字符串传递。 So, you can change your body to所以,你可以改变你的身体

[
     "const handle = () => {",
     "$0",
     "}"
]

$0 is where your cursor will be if you accept the hint.如果您接受提示,则$0是光标所在的位置。 The pressing tab will take you to $1, $2, $3, ... .按下选项卡将带您到$1, $2, $3, ...

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

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