snippet 簡介
snippet即代碼段,指的是能夠幫助輸入重復代碼模式勤婚,比如循環(huán)或條件語句的模板摹量。通過 snippet ,我們僅僅輸入一小段字符串馒胆,就可以在代碼段引擎的幫助下缨称,生成預定義的模板代碼,接著我們還可以通過在預定義的光標位置之間跳轉(zhuǎn)祝迂,來快速補全模板睦尽。
vsCode進入snippet的三種方法
1.通過快捷鍵「Ctrl + Shift + P」打開命令窗口(All Command Window),輸入「snippet」型雳,點選「首選項:配置用戶代碼段片段」当凡;
2.點擊界面最左側(cè)豎欄(也即活動欄)最下方的齒輪按鈕,在彈出來的菜單中點選「用戶代碼片段」
3.按下「Alt」鍵切換菜單欄纠俭,通過文件 > 首選項 > 用戶代碼片段宁玫;
例子簡介
在以上的輸入框中輸入vue,找到 vue.json ,然后在 vue.json 里面配置柑晒。以下是一個vue的例子
{
// Place your snippets for vue 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, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
// same ids are connected.
// Example:
// "Print to console": {
// "prefix": "log",
// "body": [
// "console.log('$1');",
// "$2"
// ],
// "description": "Log output to console"
// }
}
- prefix:前綴欧瘪,定義了 snippets 從 IntelliSense 中呼出的關(guān)鍵字
- body: 主體,即模板的主體內(nèi)容匙赞,其中每個字符串表示一行
- description:說明佛掖,會在 IntelliSense 候選欄中出現(xiàn)妖碉。未定義的情況下直接顯示對象名。
把上面的設(shè)置更改為自己想要的樣子
{ // 把example 的注釋去掉
"Print to console": {// 這個代碼段的名字,隨便起
"prefix": "log",// 綁定的關(guān)鍵字
"body": [// 輸入 Log 時芥被,生成的內(nèi)容,每行內(nèi)容包含在雙引號里欧宜,用逗號間隔
"console.log('$1');",//$1 光標出現(xiàn)的位置,如果不設(shè)置拴魄,默認出現(xiàn)在末尾
"$2"http://用tab,切換到下一個參數(shù)位置
],
"description": "Log output to console"http://對這個代碼段的簡單描述
}
}