依次 点击 ---- 文件 -> 首选项 -> 用户代码片段
然后选择一个需要创建代码块的语言,选择js
自动打开一个javascript.json 文件
// Place your snippets for JavaScript here. Each snippet is defined under a snippet name and has a prefix, body and
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, ${id} and ${id:label} and ${1:label} for variables. Variables with the same id are connected.
// Example:
"Print to console": {
"prefix": "log",
"body": [
"console.log('$1');",
"$2"
],
"description": "Log output to console"
}```
>"prefix"(前缀)对应的值,是你在文件内输入该值的时候,会出现相应的提示
>'body' 是用户自定义的代码段,点击提示后,会在文件中自动插入,是一个字符串数组,每个字符串代表每一行的代码句
body中的$id($1,$2...)表示代码在文件插入后,可以在$id处多处编辑
如果是多个$id是一样的,比如多个$1,插入代码段后,可以同时编辑该处,其他的则是按id大小依次编辑
>“description”表示提示时描述
若要添加新的代码段,至于要在{}后面添加即可,注意每个定义代码块后面需要以,分隔
自己的常用的代码段:
//常用require
"Print to common require": {
"prefix": "common require",
"body": [
"const fs = require('fs');",
"const path = require('path');",
"const iconv = require('iconv-lite');",
"const readline = require('readline');",
"$2"
],
"description": "common require"
},
//引入fs模块
"Print to fs require": {
"prefix": "fs",
"body": [
"const fs = require('fs');"
],
"description": "file system"
},
//引入fs模块
"Print to path require": {
"prefix": "path",
"body": [
"const path = require('path');"
],
"description": "path module"
},
//引入iconv模块
"Print to iconv-lite require": {
"prefix": "iconv-lite",
"body": [
"const iconv = require('iconv');"
],
"description": "iconv-lite module"
},
//引入fs模块
"Print to readline require": {
"prefix": "readline",
"body": [
"const readline = require('readline');"
],
"description": "readline modul"
},
//require
"Print to require": {
"prefix": "require",
"body": [
"const $1 = require('$1')"
],
"description": "require"
},
// 'use strict'
"Print to use strict": {
"prefix": "use strict",
"body": [
""use strict"",
"$2"
],
"description": "use strict"
},
//常用path.join
"Print to path.join": {
"prefix": "join",
"body": [
"path.join(__dirname,$1)"
],
"description": "path.join"
}
列编辑快捷键: alt + shift + 左键
格式化代码 alt shift f
网友评论