VsCode 各场景高级调试技巧!
VsCode鑷粠璇炵敓浠ユ潵锛屼互鍏跺悇鑷紭寮傜殑鐗规ц繀閫熻蛋绾傚挨鍏舵槸瀵逛簬鍓嶇寮鍙戝皬浼欎即鏉ヨ锛屽嚑涔庢垚涓哄繀涓嶅彲灏戠殑寮鍙戝伐鍏枫
鎵浠ワ紝鐔熺粌鎺屾彙VsCode鐨勫悇鑷娇鐢ㄦ妧宸т笌璋冭瘯鎶宸т細璁╀綘鐨勬棩甯稿紑鍙戝伐浣滄晥鐜囧嶅銆傛湰鏂囧皢浼氫互澶ч噺鍥炬枃鐨勬柟寮忥紝浠庝笅闈㈠嚑涓柟闈㈣缁嗕粙缁峍sCode鐨勫悇绉嶆妧宸э細
VsCode瀹夎鍚庯紝浼氳嚜鍔ㄥ啓鍏ョ幆澧冨彉閲忥紝缁堢杈撳叆
鍦ㄩ厤缃枃浠朵腑娣诲姞濡備笅閰嶇疆锛屽彲浠ュ鍔犲瓧绗︽暟鏍囧昂杈呭姪绾
涓嬮潰浠ュ湪VsCode涓揩閫熻皟璇曚竴涓狽ode椤圭洰涓轰緥锛屾紨绀烘柇鐐圭殑鍩烘湰浣跨敤銆傚悗鏂囦細缁х画缁撴潫鍚勭楂樼骇鏂偣銆
娴獥鐨勬搷浣滄寜閽姛鑳戒緷娆′负锛
鏃ュ織鏂偣鏄櫘閫氭柇鐐圭殑涓绉嶅彉浣擄紝鍖哄埆鍦ㄤ簬涓嶄細涓柇璋冭瘯锛岃屾槸鍙互鎶婁俊鎭褰曞埌鎺у埗鍙般傛棩蹇楁柇鐐瑰浜庤皟璇曟棤娉曟殏鍋滄垨鍋滄鐨勬湇鍔℃椂鐗瑰埆鏈夌敤銆傛楠ゅ涓:
鍙互浣跨敤
鏉′欢鏂偣鏄〃杈惧紡缁撴灉涓
鍙湁璇ヨ浠g爜鍛戒腑浜嗘寚瀹氭鏁帮紝鎵嶄細杩涜鏂偣銆傛楠ゅ涓:
浠呭綋鎵ц鍒拌揪涓庡唴鑱旀柇鐐瑰叧鑱旂殑鍒楁椂锛屾墠浼氬懡涓唴鑱旀柇鐐广傝繖鍦ㄨ皟璇曞湪涓琛屼腑鍖呭惈澶氫釜璇彞鐨勭缉灏忎唬鐮佹椂鐗瑰埆鏈夌敤銆傛瘮濡俧or寰幆锛岀煭璺繍绠楃绛変竴琛屼唬鐮佸寘鍚涓〃杈惧紡鏃朵細鐗瑰埆鏈夌敤銆傛楠ゅ涓:
鍙互灏嗗彉閲忔坊鍔犲埌鐩戝惉闈㈡澘锛屽疄鏃惰瀵熷彉閲忕殑鍙樺寲銆
寮鍙 Web 绋嬪簭閫氬父闇瑕佸湪 Web 娴忚鍣ㄤ腑鎵撳紑鐗瑰畾 URL锛屼互渚垮湪璋冭瘯鍣ㄤ腑璁块棶鏈嶅姟鍣ㄤ唬鐮併俈S Code 鏈変竴涓唴缃姛鑳解 serverReadyAction 鈥濇潵鑷姩鍖栬繖涓换鍔°
鍏充簬NodeJs椤圭洰鐨勮皟璇曟柟娉曪紝宸茬粡鍦ㄤ笂杩扮殑鏂偣鐨勫熀鏈娇鐢[1]閮ㄥ垎鍋氫簡浠嬬粛锛屽彲浠ョ綉涓婃粴鍔ㄧ炕闃呫
娉ㄦ剰锛屽鏋滅紪璇戝悗鐨刯s鏂囦欢涓嶅湪鐩稿簲鐨勪綅缃紝閫氳繃鍥句腑鐨
淇敼宸ヤ綔鍖簍s鐗堟湰鐨勬柟娉曪細
瀛︿細浜嗕笂杩皌s鐨勮皟璇曞悗锛屾垜浠皾璇曡皟璇昲tml鏂囦欢锛屽苟涓攈tml鏂囦欢涓紩鍏s鏂囦欢:
涓嬮潰浠嬬粛涓ょ璋冭瘯vue2椤圭洰鐨3绉嶆柟娉曪紝鍏朵粬妗嗘灦鐨勮皟璇曚篃绫讳技锛
璇ヨ剼鏈殑浣滅敤鏄繍琛
娉ㄦ剰锛氭鏂瑰紡鐨勪富瑕佺偣鍦ㄤ簬
鏇村璇︾粏鍐呭锛屽ぇ瀹跺彲浠ョ偣鍑昏繖閲岀殑鍙傝冩枃妗[2]鏌ラ槄銆
鏇村璇︾粏鍐呭锛岃鐐瑰嚮杩欓噷鐨勫弬鑰冩枃妗[3]鏌ラ槄銆
鍊熷姪vscode鎻掍欢
Electron寰堝浜洪兘浣跨敤杩囷紝涓昏鐢ㄤ簬寮鍙戣法骞冲彴鐨勭郴缁熸闈㈠簲鐢ㄣ傞偅涔堟潵鐪嬩笅
姝ゅ閰嶇疆浜嗕袱涓皟璇曞懡浠:
鍦ㄤ富杩涚▼鐩稿叧浠g爜涓婃墦涓婃柇鐐癸紝鐒跺悗鍚姩璋冭瘯涓昏繘绋嬪懡浠ゅ氨鍙互璋冭瘯涓昏繘绋嬩簡
娉ㄦ剰锛岃繖閲岀殑
鍒囨崲鍒版覆鏌撹繘绋嬬殑璋冭瘯閫夐」锛屽湪娓叉煋杩涚▼鐨勪唬鐮佸鎵撲笂鏂偣锛岀偣鍑昏皟璇曘傛敞鎰忥紝姝ゆ椂骞朵笉浼氭湁鏂偣缁堢锛岄渶瑕
鍚屾椂寮鍚覆鏌撹繘绋嬪拰涓昏繘绋嬬殑璋冭瘯锛屽彧闇瑕佸垏鎹㈠埌璋冭瘯鍏ㄩ儴鐨勯夐」鍗冲彲銆傛敞鎰忥紝姝ょ鏂瑰紡鍥犱负
鏇村璋冭瘯Electron鐨勫唴瀹癸紝鍙互鐐瑰嚮鍙傝冩枃妗[4]鏌ラ槄銆
鐢ㄤ袱涓珫绾垮寘鍚涓夋嫨鍊硷紝
鍙渶瑕侀夋嫨
vscode鍐呯疆浜嗗Emmet鐨勬敮鎸侊紝鏃犻渶棰濆鎵╁睍銆備緥濡俬tml鐨凟mmet婕旂ず濡備笅:
閫変腑鎴栬呭厜鏍囨墍澶勭殑浣嶇疆锛屾寜
VsCode鎵╁睍鎻掍欢鍙互鍋氫粈涔堜簨鎯咃紵
鍩轰簬
浣滆咃細鎰i敜@https://juejin.cn/post/7071146744339234846
鎵浠ワ紝鐔熺粌鎺屾彙VsCode鐨勫悇鑷娇鐢ㄦ妧宸т笌璋冭瘯鎶宸т細璁╀綘鐨勬棩甯稿紑鍙戝伐浣滄晥鐜囧嶅銆傛湰鏂囧皢浼氫互澶ч噺鍥炬枃鐨勬柟寮忥紝浠庝笅闈㈠嚑涓柟闈㈣缁嗕粙缁峍sCode鐨勫悇绉嶆妧宸э細
- 绗竴閮ㄥ垎涓昏浠嬬粛VsCode鐨勫熀鏈妧宸э紝姣斿甯哥敤蹇嵎閿佽緟鍔╂爣灏虹瓑銆傜啛鎮夋閮ㄥ垎鐨勫彲浠ョ洿鎺ヨ烦杩囥
- 绗簩閮ㄥ垎涓昏鍚勭鏂偣锛堟瘮濡傛棩蹇楁柇鐐广佸唴鑱旀柇鐐广佽〃杈惧紡鏂偣绛夌瓑锛夈佹暟鎹潰鏉跨瓑绛
- 绗笁閮ㄥ垎涓昏璁茶В鍚勭椤圭洰鐨勮皟璇曞疄鎴橈紝姣斿Node绋嬪簭銆乀S绋嬪簭銆乂ue绋嬪簭銆丒lectron绋嬪簭銆丠tml绛夌殑璋冭瘯瀹炴垬
- 鏈鍚庝竴閮ㄥ垎灏嗕細璁茶В鍏朵粬鏈夌敤鐨勬妧宸э紝姣斿浠g爜鐗囨銆侀噸鏋勩丒mmet绛夌瓑
鍩烘湰鎶宸
蹇熷惎鍔
VsCode瀹夎鍚庯紝浼氳嚜鍔ㄥ啓鍏ョ幆澧冨彉閲忥紝缁堢杈撳叆
code
鍗冲彲鍞よ捣VsCode搴旂敤绋嬪簭銆甯哥敤蹇嵎閿
ctrl + p
蹇熸悳绱㈡枃浠跺苟璺宠浆锛屾坊鍔:
鍙互璺宠浆鍒版寚瀹氳
ctrl + shift + p
鏍规嵁鎮ㄥ綋鍓嶇殑涓婁笅鏂囪闂墍鏈夊彲鐢ㄥ懡浠ゃctrl + shift + c
鍦ㄥ閮ㄦ墦寮缁堢骞跺畾浣嶅埌褰撳墠椤圭洰璺緞ctrl + 鎸夐敭1宸﹁竟鐨勭鍙
鏄剧ず闅愯棌缁堢闈㈡澘Ctrl+B
鍒囨崲渚ц竟鏍Ctrl+\
蹇熸媶鍒嗘枃浠剁紪杈alt + 鍗曟満宸﹂敭
娣诲姞澶氬鍏夋爣alt + shift + 鍗曞嚮宸﹂敭
鍚屼竴鍒楁墍鏈変綅缃坊鍔犲厜鏍alt + shift + 榧犳爣閫夋嫨
閫夋嫨鐩稿悓寮濮嬪拰缁撴潫鐨勫尯鍩
alt + 涓婇敭鎴栦笅閿
灏嗗綋鍓嶈鎴栬呴変腑鐨勫尯鍩熶笂绉/涓嬬Щ涓琛
鍨傜洿鏍囧昂
鍦ㄩ厤缃枃浠朵腑娣诲姞濡備笅閰嶇疆锛屽彲浠ュ鍔犲瓧绗︽暟鏍囧昂杈呭姪绾
"editor.rulers": [40, 80, 100]
澶嶅埗浠g爜
杩涢樁鎶宸
鏂偣鐨勫熀鏈娇鐢
涓嬮潰浠ュ湪VsCode涓揩閫熻皟璇曚竴涓狽ode椤圭洰涓轰緥锛屾紨绀烘柇鐐圭殑鍩烘湰浣跨敤銆傚悗鏂囦細缁х画缁撴潫鍚勭楂樼骇鏂偣銆
- 鍒涘缓涓涓熀鏈殑node椤圭洰涓篘odejs
- 鎵撳紑宸︿晶璋冭瘯闈㈡澘锛岄夋嫨浣犺璋冭瘯鐨刵ode椤圭洰鍚嶇О锛屾坊鍔犺皟璇曢厤缃
- 閫夋嫨璋冭瘯鐨勯」鐩被鍨嬩负Node.js
- 鎵撳紑鐢熸垚鐨.vscode/launch.json鏂囦欢锛屾寚瀹氱▼搴忓叆鍙f枃浠
program
瀛楁鐢ㄤ簬鎸囧畾浣犵殑绋嬪簭鍏ュ彛鏂囦欢锛${workspaceFolder}
琛ㄧず褰撳墠椤圭洰鏍硅矾寰- 鍦ㄧ▼搴忎腑娣诲姞鏂偣锛屽彧闇瑕佺偣鍑诲乏渚х殑杈规爮鍗冲彲娣诲姞鏂偣
- 鎸
F5
寮濮嬭皟璇曪紝鎴愬姛璋冭瘯浼氭湁娴獥鎿嶄綔鏍
娴獥鐨勬搷浣滄寜閽姛鑳戒緷娆′负锛
- 缁х画(
F5
)銆 - 璋冭瘯涓嬩竴姝(
F10
)銆 - 鍗曟璺冲叆(
F11
)銆 - 鍗曟璺冲嚭(
Shift F11
)銆 - 閲嶆柊璋冭瘯(
Ctrl + Shift + F5
)銆 - 缁撴潫璋冭瘯(
Shift + F5
)
鏃ュ織鏂偣
鏃ュ織鏂偣鏄櫘閫氭柇鐐圭殑涓绉嶅彉浣擄紝鍖哄埆鍦ㄤ簬涓嶄細涓柇璋冭瘯锛岃屾槸鍙互鎶婁俊鎭褰曞埌鎺у埗鍙般傛棩蹇楁柇鐐瑰浜庤皟璇曟棤娉曟殏鍋滄垨鍋滄鐨勬湇鍔℃椂鐗瑰埆鏈夌敤銆傛楠ゅ涓:
- 娣诲姞鏃ュ織鏂偣鐨勬楠
- 杈撳叆瑕佹棩蹇楁柇鐐圭殑淇℃伅锛岀偣鍑诲洖杞︽坊鍔犲畬鎴
鍙互浣跨敤
{}
浣跨敤鍙橀噺锛屾瘮濡鍦ㄦ澶勬坊鍔犳棩蹇楁柇鐐癸紝b鐨勫间负${b}
- 鏃ュ織鏂偣娣诲姞鎴愬姛鍚庝細鏈夋槸涓涓彵褰㈠浘鏍
- 鎸
F5
杩愯鏌ョ湅璋冭瘯缁撴灉
琛ㄨ揪寮忔潯浠舵柇鐐
鏉′欢鏂偣鏄〃杈惧紡缁撴灉涓
true
鏃舵墠浼氳繘琛屾柇鐐癸紝姝ラ濡備笅:- 鍦ㄤ唬鐮佽宸︿晶鍙冲嚮锛屼篃鍙互娣诲姞鏂偣锛屾澶勯夋嫨娣诲姞鏉′欢鏂偣
- 濉啓琛ㄨ揪寮忥紝鎸夊洖杞﹂敭
- 娣诲姞鎴愬姛鐨勫皬鍥炬爣濡備笅
- 鎸
F5
璋冭瘯锛屾潯浠舵垚绔嬫墍浠ヨ繘琛屼簡鏂偣
鍛戒腑璁℃暟鏂偣
鍙湁璇ヨ浠g爜鍛戒腑浜嗘寚瀹氭鏁帮紝鎵嶄細杩涜鏂偣銆傛楠ゅ涓:
- 閫夋嫨鏉′欢鏂偣锛屽垏鎹负鍛戒腑娆℃暟閫夐」锛屽~鍐欏懡涓鏁
- 濉啓鎴愬姛濡備笅鍥炬墍绀
- 鎸
F5
璋冭瘯锛屽鍥炬墍绀猴紝index涓9鏃舵墠涓柇
鍐呰仈鏂偣
浠呭綋鎵ц鍒拌揪涓庡唴鑱旀柇鐐瑰叧鑱旂殑鍒楁椂锛屾墠浼氬懡涓唴鑱旀柇鐐广傝繖鍦ㄨ皟璇曞湪涓琛屼腑鍖呭惈澶氫釜璇彞鐨勭缉灏忎唬鐮佹椂鐗瑰埆鏈夌敤銆傛瘮濡俧or寰幆锛岀煭璺繍绠楃绛変竴琛屼唬鐮佸寘鍚涓〃杈惧紡鏃朵細鐗瑰埆鏈夌敤銆傛楠ゅ涓:
- 鍦ㄦ寚瀹氫綅缃寜
Shift + F9
- 璋冭瘯涔嬪悗锛屾瘡娆¤繍琛屽埌璇ュ唴鑱斿鐨勪唬鐮侀兘浼氫腑鏂
琛ュ厖鐭ヨ瘑鐐癸細鏁版嵁闈㈡澘浠嬬粛
- 鏁版嵁闈㈡澘鍙互鏌ョ湅鎵鏈夊彉閲
- 鍦ㄥ彉閲忎笂鐐瑰嚮鍙抽敭锛屽彲浠ヨ缃彉閲忓笺佸鍒跺彉閲忓肩瓑鎿嶄綔
- 鑱氱劍浜庢暟鎹潰鏉挎椂锛屽彲浠ラ氳繃閿叆鍊兼潵鎼滅储杩囨护銆傜偣鍑讳笅鍥炬墍绀烘寜閽彲浠ユ帶鍒舵槸鍚︾瓫閫夈
琛ュ厖鐭ヨ瘑鐐癸細鐩戝惉闈㈡澘浠嬬粛
鍙互灏嗗彉閲忔坊鍔犲埌鐩戝惉闈㈡澘锛屽疄鏃惰瀵熷彉閲忕殑鍙樺寲銆
- 鍦ㄥ彉閲忛潰鏉块氳繃鍙抽敭閫夋嫨鈥滄坊鍔犲埌鐩戣鈥濆皢鍙橀噺娣诲姞鍒扮洃鍚潰鏉
- 涔熷彲浠ョ洿鎺ュ湪鐩戝惉闈㈡澘閫夋嫨娣诲姞鎸夐挳杩涜鍙橀噺娣诲姞
- 娣诲姞鍙橀噺鍚庡氨鍙互瀹炴椂鐩戝惉鍙橀噺鐨勫彉鍖
琛ュ厖鐭ヨ瘑鐐癸細璋冭瘯鏈嶅姟鍣ㄦ椂鎵撳紑涓涓猆RI
寮鍙 Web 绋嬪簭閫氬父闇瑕佸湪 Web 娴忚鍣ㄤ腑鎵撳紑鐗瑰畾 URL锛屼互渚垮湪璋冭瘯鍣ㄤ腑璁块棶鏈嶅姟鍣ㄤ唬鐮併俈S Code 鏈変竴涓唴缃姛鑳解 serverReadyAction 鈥濇潵鑷姩鍖栬繖涓换鍔°
- 涓娈电畝鍗曠殑server浠g爜
var express = require('express');
var app = express();
app.get('/', function(req, res) {
res.send('Hello World!');
});
app.listen(3000, function() {
console.log('Example app listening on port 3000!');
});
澶嶅埗浠g爜
- 閰嶇疆launch.json锛屼互鏀寔鎵撳紑URI
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/app.js",
"serverReadyAction": {
"pattern": "listening on port ([0-9]+)",
"uriFormat": "http://localhost:%s",
"action": "openExternally"
}
}
澶嶅埗浠g爜
pattern
鏄缃尮閰嶇殑绋嬪害绔彛鍙凤紝绔彛鍙锋斁鍦ㄥ皬鎷彿鍐咃紝鍗充綔涓轰竴涓鍒欑殑鎹曡幏缁勪娇鐢ㄣuriFormat
鏄犲皠涓篣RI锛屽叾涓%s
浣跨敤pattern
涓殑绗竴涓崟鑾风粍鏇挎崲銆傛渶鍚庝娇鐢ㄨURI浣滀负澶栭儴绋嬪簭鎵撳紑鐨刄RI銆- 鎸
F5
璋冭瘯锛屼細鑷姩鎵撳紑娴忚鍣紝涓斾細鍦ㄤ笅鍥炬墍绀哄涓柇锛屽綋缁х画鎵ц鍚庯紝娴忚鍣ㄦ墠鑳界湅鍒拌緭鍑轰簡server鐨勫唴瀹
缁堝眬锛氬悇鍦烘櫙璋冭瘯瀹炴垬
璋冭瘯NodeJS椤圭洰
鍏充簬NodeJs椤圭洰鐨勮皟璇曟柟娉曪紝宸茬粡鍦ㄤ笂杩扮殑鏂偣鐨勫熀鏈娇鐢[1]閮ㄥ垎鍋氫簡浠嬬粛锛屽彲浠ョ綉涓婃粴鍔ㄧ炕闃呫
璋冭瘯Typescript椤圭洰
璋冭瘯TS椤圭洰鍓嶏紝鍏堝垱寤轰竴涓猅S椤圭洰# 缁堢杩愯
tsc --init
澶嶅埗浠g爜
VS Code 鍐呯疆浜嗗 Ts 璋冭瘯鐨勬敮鎸併備负浜嗘敮鎸佽皟璇 Ts 涓庢鍦ㄦ墽琛岀殑 Js 浠g爜鐩哥粨鍚堬紝VS Code 渚濊禆浜庤皟璇曞櫒鐨剆ource map鍦 Ts 婧愪唬鐮佸拰姝e湪杩愯鐨 Js 涔嬮棿杩涜鏄犲皠锛屾墍浠ラ渶瑕侀渶瑕佸紑鍚sourceMap
閫夐」銆{
"sourceMap": true,
"outDir": "./out"
}
澶嶅埗浠g爜const num: number = 123;
console.log(num);
function fn(arg: string): void {
console.log('fn', arg);
}
fn("Hello");
澶嶅埗浠g爜- 鏂板缓index.ts鏂囦欢锛屽啓涓涓熀鏈殑ts浠g爜
- 鎵撳紑
tsconfig.json
鏂囦欢锛屽紑鍚痵ourceMap閫夐」鍜屾寚瀹氱紪璇戝悗杈撳嚭鐨勮矾寰 - 鍏堝垵濮嬪寲涓涓猼s绋嬪簭锛岀敓鎴愰粯璁ょ殑
tsconfig.json
鏂囦欢
鎵嬪姩缂栬瘧璋冭瘯TS
鍦ㄤ笂杩扮殑ts鍩烘湰椤圭洰涓細- 鎸
F5
鎴栬杩愯 -> 鍚姩璋冭瘯
锛屾鏃跺彲浠ョ湅鍒板彲浠ユ甯竏ebug璋冭瘯 - 鍦╥ndex.ts涓殢鎰忔坊鍔犱竴涓柇鐐
- 姝ゆ椂鍙互鐪嬪埌鐢熸垚浜唎ut鏂囦欢澶癸紝閲岄潰鍖呭惈涓涓
index.js
鍜屼竴涓index.js.map
鏂囦欢 - 缁堢鎵цts鐨勭紪璇戝懡浠
tsc
閫氳繃鏋勫缓浠诲姟鏋勫缓璋冭瘯TS
娉ㄦ剰锛屽鏋滀綘浣跨敤鐨勬槸鍏朵粬缁堢(姣斿cmder
)鐨勮瘽锛屾湁鍙兘浼氱敓鎴愪笉浜嗭紝濡備笅鍥炬墍绀猴紝浣跨敤榛樿鐨刾owershell鍗冲彲锛- 璋冭瘯鐨勮瘽鍜屼笂杩版楠や竴鏍凤紝鍦ㄦ湁浜嗙紪璇戝悗鐨勬枃浠跺悗锛屾寜
F5
鍗冲彲 - 閫夋嫨
tsc鏋勫缓閫夐」
锛屾鏃跺彲浠ョ湅鍒拌嚜鍔ㄧ敓鎴愪簡缂栬瘧鏂囦欢 - 鎸
Ctrl+Shift+B
鎴栭夋嫨缁堢 -> 杩愯鐢熸垚浠诲姟
锛屾鏃朵細寮瑰嚭涓涓笅鎷夎彍鍗
鐩戣鏀瑰彉骞跺疄鏃剁紪璇- 濡備笅鍥炬墍绀猴紝浼氬疄鏃剁紪璇
- 鎸
Ctrl + Shift + B
閫夋嫨鐩戣閫夐」锛屽彲浠ュ疄鏃剁洃瑙嗘枃浠跺唴瀹瑰彂鐢熷彉鍖栵紝閲嶆柊缂栬瘧
琛ュ厖鐭ヨ瘑鐐癸細tasks閰嶇疆鏂囦欢鐨勫垱寤烘柟寮
- 鏂规硶1锛氱偣鍑
缁堢 -> 閰嶇疆浠诲姟 -> 閫夋嫨浠诲姟
鍙互鐢熸垚瀵瑰簲鐨則asks.json閰嶇疆
- 鏂规硶2锛氱偣鍑
缁堢 -> 杩愯鐢熸垚浠诲姟 -> 鐐瑰嚮璁剧疆鍥炬爣
涔熷彲浠ョ敓鎴愬搴旂殑tasks.json閰嶇疆
琛ュ厖鐭ヨ瘑鐐癸細姣忔璋冭瘯鏃堕噸鏂扮紪璇
- 鎸変笂杩扮殑鎿嶄綔宸茬粡鐢熸垚浜唗ask.json閰嶇疆鏂囦欢
{
"version": "2.0.0",
"tasks": [
{
"type": "typescript",
"tsconfig": "tsconfig.json",
"problemMatcher": [
"$tsc"
],
"group": "build",
"label": "tsc: 鏋勫缓 - tsconfig.json"
}
]
}
澶嶅埗浠g爜
- 鐐瑰嚮
杩愯 -> 娣诲姞閰嶇疆 -> 閫夋嫨nodejs
- 鍦ㄧ敓鎴愮殑
launch.json
鏂囦欢涓紝娣诲姞preLaunchTask
瀛楁锛屽兼槸tasks.json
鐨label
鍊硷紝涓瀹氳鐩稿悓锛屾敞鎰忓ぇ灏忓啓銆傝瀛楁鐨勪綔鐢ㄦ槸鍦ㄦ墽琛屽懡浠ゅ墠鍏堟墽琛屾敼task
浠诲姟銆
娉ㄦ剰锛屽鏋滅紪璇戝悗鐨刯s鏂囦欢涓嶅湪鐩稿簲鐨勪綅缃紝閫氳繃鍥句腑鐨
outFiles
瀛楁鍙互鎸囧畾ts
缂栬瘧鍚庣殑js
璺緞銆- 鍦
index.ts
鏂囦欢涓寜F5
鍚姩璋冭瘯锛屽彲浠ョ湅鍒拌皟璇曞墠宸茬粡鐢熸垚浜嗙紪璇戞枃浠讹紝鑰屽悗灏卞彲浠ユ甯歌皟璇曚簡銆
琛ュ厖鐭ヨ瘑鐐癸細VsCode鐨凾S鐗堟湰璇存槑
vscode鏈韩鍐呯疆浜嗗ts鐨勬敮鎸
vscode鍐呯疆鐨則s鐗堟湰锛堝嵆宸ヤ綔鍖虹増鏈級锛屼粎浠呯敤浜嶪ntelliSense锛堜唬鐮佹彁绀猴級锛屽伐浣滃尯ts鐗堟湰涓庣敤浜庣紪璇戠殑ts鐗堟湰鏃犱换浣曞叧绯汇
淇敼宸ヤ綔鍖簍s鐗堟湰鐨勬柟娉曪細
- 鍦ㄧ姸鎬佹爮閫夋嫨typescript鐨勫浘鏍囷紝閫夋嫨鐗堟湰鍒囨崲
- 閫夋嫨浣犻渶瑕佺殑鐗堟湰鍗冲彲
璋冭瘯html椤圭洰
瀛︿細浜嗕笂杩皌s鐨勮皟璇曞悗锛屾垜浠皾璇曡皟璇昲tml鏂囦欢锛屽苟涓攈tml鏂囦欢涓紩鍏s鏂囦欢:
- 鍒涘缓html锛屽紩鍏s缂栬瘧鍚庣殑js鏂囦欢
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h3>Hello</h3>
<script src="./out/index.js"></script>
</body>
</html>
澶嶅埗浠g爜
- ts婧愭枃浠跺涓嬶細
const num: number = 1221;
console.log(num);
function fn(arg: string): void {
console.log('fn', arg);
}
document.body.append('World')
fn("he");
澶嶅埗浠g爜
- 鎵揹ebug
- launch.json鍚姩鍛戒护閰嶇疆
{
// 浣跨敤 IntelliSense 浜嗚В鐩稿叧灞炴с
// 鎮仠浠ユ煡鐪嬬幇鏈夊睘鎬х殑鎻忚堪銆
// 娆蹭簡瑙f洿澶氫俊鎭紝璇疯闂: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "pwa-chrome",
"request": "launch",
"name": "Launch Chrome",
"url": "file:///E:/demo/vscode/debug/ts/index.html",
"preLaunchTask": "tsc: 鏋勫缓 - tsconfig.json",
"webRoot": "${workspaceFolder}"
}
]
}
澶嶅埗浠g爜
- 閫夋嫨鎴戜滑鐨勫惎鍔ㄥ懡浠
- 鎸
F5
鍙互姝e父鍞よ捣chrome娴忚鍣紝骞跺湪vscode鐨則s婧愮爜澶勪細鏈塪ebug鏁堟灉
璋冭瘯Vue椤圭洰鐨勪袱绉嶆柟寮
涓嬮潰浠嬬粛涓ょ璋冭瘯vue2椤圭洰鐨3绉嶆柟娉曪紝鍏朵粬妗嗘灦鐨勮皟璇曚篃绫讳技锛
涓嶄娇鐢╲scode鎻掍欢Debugger for chrome鐨勬柟娉
- 鍒濆鍖杤ue椤圭洰锛岄厤缃
vue.config.js
锛屾寚瀹氳鐢熸垚sourceMaps璧勬簮
module.exports = {
configureWebpack: {
// 鐢熸垚sourceMaps
devtool: "source-map"
}
};
澶嶅埗浠g爜
- 鏍圭洰褰曚笅鍒涘缓
./vscode/launch.json鏂囦欢
鎴栬呴夋嫨杩愯 -> 娣诲姞閰嶇疆 -> Chrome
{
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "launch",
"name": "vuejs: chrome",
"url": "http://localhost:8080",
"webRoot": "${workspaceFolder}",
"breakOnLoad": true,
"pathMapping": {
"/_karma_webpack_": "${workspaceFolder}"
},
"sourceMapPathOverrides": {
"webpack:/*": "${webRoot}/*",
"/./*": "${webRoot}/*",
"/src/*": "${webRoot}/*",
"/*": "*",
"/./~/*": "${webRoot}/node_modules/*"
},
"preLaunchTask": "serve"
}
]
}
澶嶅埗浠g爜
- 娣诲姞浠诲姟鑴氭湰
{
"version": "2.0.0",
"tasks": [
{
"label": "serve",
"type": "npm",
"script": "serve",
"isBackground": true,
"problemMatcher": [
{
"base": "$tsc-watch",
"background": {
"activeOnStart": true,
"beginsPattern": "Starting development server",
"endsPattern": "Compiled successfully"
}
}
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
澶嶅埗浠g爜
璇ヨ剼鏈殑浣滅敤鏄繍琛
npm run serve
缂栬瘧鍛戒护銆- 鎸
F5
鍚姩璋冭瘯鍗冲彲
娉ㄦ剰锛氭鏂瑰紡鐨勪富瑕佺偣鍦ㄤ簬
launch.json
閰嶇疆鏂囦欢涓紝閫氳繃preLaunchTask
瀛楁鎸囧畾璋冭瘯鍓嶅厛杩愯涓涓换鍔¤剼鏈紝preLaunchTask
鐨勫煎搴tasks.json
鏂囦欢涓殑label
鍊笺鏇村璇︾粏鍐呭锛屽ぇ瀹跺彲浠ョ偣鍑昏繖閲岀殑鍙傝冩枃妗[2]鏌ラ槄銆
鍊熷姪vscode鎻掍欢Debugger for Chrome鍦–hrome涓皟璇
- 绗竴姝ヨ繕鏄垵濮嬪寲vue椤圭洰锛屾坊鍔
vue.config.js
鏂囦欢閰嶇疆锛屾寚瀹氳鐢熸垚sourceMaps璧勬簮
module.exports = {
configureWebpack: {
// 鐢熸垚sourceMaps
devtool: "source-map"
}
};
澶嶅埗浠g爜
- vscode涓墿灞曚腑瀹夎
Debugger for Chrome
鎻掍欢锛屽苟纭繚娌℃湁绂佺敤鎻掍欢
- 鎵嬪姩鍚姩椤圭洰鐨勮繍琛, 姝ゆ柟寮忎笉闇瑕侀厤缃
tasks.json
浠诲姟
# 缁堢鎵ц鍛戒护锛屽惎鍔ㄩ」鐩
npm run serve
澶嶅埗浠g爜
- 鎸
F5
鍚姩璋冭瘯鍗冲彲
鏇村璇︾粏鍐呭锛岃鐐瑰嚮杩欓噷鐨勫弬鑰冩枃妗[3]鏌ラ槄銆
鍊熷姪vscode鎻掍欢Debugger for Firfox
鍦‵irefox涓皟璇
- 鍜
Debugger for Chrome
鍩烘湰涓鏍凤紝鍖哄埆鍦ㄤ簬瀹夎Debugger for Firfox
鎻掍欢锛屽苟鍦╨aunch.json閰嶇疆涓紝澧炲姞璋冭瘯Firefox鐨勯厤缃嵆鍙紝閰嶇疆濡備笅
{
"version": "0.2.0",
"configurations": [
// 鐪佺暐Chrome鐨勯厤缃...
// 涓嬮潰娣诲姞鐨凢irefox鐨勯厤缃
{
"type": "firefox",
"request": "launch",
"reAttach": true,
"name": "vuejs: firefox",
"url": "http://localhost:8080",
"webRoot": "${workspaceFolder}/src",
"pathMappings": [{ "url": "webpack:///src/", "path": "${webRoot}/" }]
}
]
}
澶嶅埗浠g爜
- 璋冭瘯鏃堕夋嫨瀵瑰簲鐨勮皟璇曞懡浠ゅ嵆鍙
Firefox鍒濆鍚姩鏃朵笉浼氳Е鍙戣皟璇曪紝闇瑕佸埛鏂颁竴娆
璋冭瘯Electron椤圭洰
Electron寰堝浜洪兘浣跨敤杩囷紝涓昏鐢ㄤ簬寮鍙戣法骞冲彴鐨勭郴缁熸闈㈠簲鐢ㄣ傞偅涔堟潵鐪嬩笅
vue-cli-electron-builder
鍒涘缓鐨Electron
椤圭洰鎬庝箞璋冭瘯銆傛楠ゅ涓嬶細- 鍦ㄥ垵濮嬪寲椤圭洰鍚庯紝棣栧厛淇敼
vue.config.js
鏂囦欢閰嶇疆锛屽鍔爏ourceMaps閰嶇疆锛
module.exports = {
configureWebpack: {
devtool: 'source-map'
}
}
澶嶅埗浠g爜
- 鍒涘缓璋冭瘯閰嶇疆
.vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Electron: Main",
"type": "node",
"request": "launch",
"protocol": "inspector",
"preLaunchTask": "bootstarp-service",
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron",
"windows": {
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron.cmd"
},
"args": ["--remote-debugging-port=9223", "./dist_electron"],
"outFiles": ["${workspaceFolder}/dist_electron/**/*.js"]
},
{
"name": "Electron: Renderer",
"type": "chrome",
"request": "attach",
"port": 9223,
"urlFilter": "http://localhost:*",
"timeout": 0,
"webRoot": "${workspaceFolder}/src",
"sourceMapPathOverrides": {
"webpack:///./src/*": "${webRoot}/*"
}
},
],
"compounds": [
{
"name": "Electron: All",
"configurations": ["Electron: Main", "Electron: Renderer"]
}
]
}
澶嶅埗浠g爜
姝ゅ閰嶇疆浜嗕袱涓皟璇曞懡浠:
Electron: Main
鐢ㄤ簬璋冭瘯涓昏繘绋嬶紝Electron: Renderer
鐢ㄤ簬璋冭瘯娓叉煋杩涚▼锛compounds[].
閫夐」鐢ㄤ簬瀹氫箟澶嶅悎璋冭瘯閫夐」; configurations
瀹氫箟鐨勫鍚堝懡浠ゆ槸骞惰鐨; preLaunchTask
鐢ㄤ簬閰嶇疆鍛戒护鎵ц鍓嶅厛鎵ц鐨勪换鍔¤剼鏈紝鍏跺煎搴tasks.json
涓殑label
瀛楁; preLaunchTask
鐢ㄥ湪compounds
鏃讹紝鐢ㄤ簬瀹氫箟configurations
澶嶅悎浠诲姟鎵ц鍓嶅厛鎵ц鐨勮剼鏈- 鍒涘缓浠诲姟鑴氭湰
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "bootstarp-service",
"type": "process",
"command": "./node_modules/.bin/vue-cli-service",
"windows": {
"command": "./node_modules/.bin/vue-cli-service.cmd",
"options": {
"env": {
"VUE_APP_ENV": "dev",
"VUE_APP_TYPE": "local"
}
}
},
"isBackground": true,
"args": [
"electron:serve", "--debug"
],
"problemMatcher": {
"owner": "custom",
"pattern": {
"regexp": ""
},
"background": {
"beginsPattern": "Starting development server\\.\\.\\.",
"endsPattern": "Not launching electron as debug argument was passed\\."
}
}
}
]
}
澶嶅埗浠g爜
- 鍚姩璋冭瘯
鍦ㄤ富杩涚▼鐩稿叧浠g爜涓婃墦涓婃柇鐐癸紝鐒跺悗鍚姩璋冭瘯涓昏繘绋嬪懡浠ゅ氨鍙互璋冭瘯涓昏繘绋嬩簡
娉ㄦ剰锛岃繖閲岀殑
options
鍙傛暟鏄牴鎹疄闄呯殑鎯呭喌锛岃嚜瀹氫箟娣诲姞鎴戜滑杩愯椤圭洰鏃舵墍闇瑕佺殑鍙傛暟锛屾瘮濡傛垜杩欓噷鍥犱负鍚姩椤圭洰鐨刵pm鍛戒护鏄:"serve-local:dev": "cross-env VUE_APP_TYPE=local VUE_APP_ENV=dev vue-cli-service electron:serve"
澶嶅埗浠g爜
- 涓昏繘绋嬭皟璇曟垚鍔
- 寮濮嬭皟璇曟覆鏌撹繘绋
鍒囨崲鍒版覆鏌撹繘绋嬬殑璋冭瘯閫夐」锛屽湪娓叉煋杩涚▼鐨勪唬鐮佸鎵撲笂鏂偣锛岀偣鍑昏皟璇曘傛敞鎰忥紝姝ゆ椂骞朵笉浼氭湁鏂偣缁堢锛岄渶瑕
ctrl+r
鎵嬪姩鍒锋柊杞欢杩涚▼鎵嶄細鐪嬪埌娓叉煋杩涚▼鐨勬柇鐐广- 鍒锋柊娓叉煋杩涚▼鍚庣殑鏁堟灉锛屽涓嬪浘锛屽凡缁忚繘鍏ヤ簡鏂偣
- 鍙︿竴绉嶆柟寮
鍚屾椂寮鍚覆鏌撹繘绋嬪拰涓昏繘绋嬬殑璋冭瘯锛屽彧闇瑕佸垏鎹㈠埌璋冭瘯鍏ㄩ儴鐨勯夐」鍗冲彲銆傛敞鎰忥紝姝ょ鏂瑰紡鍥犱负
compounds[].configurations
閰嶇疆鏄苟琛屾墽琛岀殑锛屽苟涓嶄竴瀹氳兘淇濊瘉娓叉煋杩涚▼璋冭瘯涓瀹氳兘闄勫姞鍒颁富杩涚▼璋冭瘯鎴愬姛锛堜及璁℃槸鏃舵満闂锛夛紝鏈変簺鏃跺欎細璋冭瘯娓叉煋杩涚▼涓嶆垚鍔熴傛墍浠ワ紝鍙互閲囧彇涓婇潰鐨勬柟寮忚繘琛岃皟璇曘鏇村璋冭瘯Electron鐨勫唴瀹癸紝鍙互鐐瑰嚮鍙傝冩枃妗[4]鏌ラ槄銆
琛ュ厖锛氭洿杩涗竴姝
- VS璋冭瘯React app鏂囨。[5]
- VS璋冭瘯Next.js鏂囨。[6]
- 鏇村...[7]
鍏朵粬鎶宸
鎶宸т竴锛氫唬鐮佺墖娈碉紙snippets锛
浠庢墿灞曞晢搴椾腑瀹夎snippets
@category:"snippets"
澶嶅埗浠g爜
鍒涘缓鍏ㄥ眬浠g爜鐗囨
- 閫夋嫨
鏂囦欢 -> 棣栭夐」 -> 鐢ㄦ埛鐗囨
- 閫夋嫨
鏂板缓鍏ㄥ眬浠g爜鐗囨鏂囦欢
娣诲姞浠g爜鐗囨鏂囦欢鐨勬枃浠跺悕绉帮紝浼氱敓鎴.code-snippets
鍚庣紑鐨勬枃浠- 瀹氫箟鐢ㄦ埛鐗囨
{
"鑷姩琛ュ叏console.log": {
"scope": "javascript,typescript",
"prefix": "log",
"body": [
"console.log('$1');",
"$2"
],
"description": "杈撳嚭console.log('')"
}
}
澶嶅埗浠g爜
鍏抽敭璇 | 绫诲瀷 | 璇存槑 |
---|---|---|
scope | string | 浠g爜鐗囨鐢熸晥鐨勪綔鐢ㄥ煙锛屽彲浠ユ槸澶氫釜璇█锛屾瘮濡javascript,typescript 琛ㄧず鍦╦s鍜宼s鐢熸晥锛屼笉鍔scope 瀛楁琛ㄧず瀵规墍鏈夋枃浠剁被鍨嬬敓鏁 |
prefix | `string | string[]` |
body | string[] | 浠g爜鐗囨鍐呭锛屾暟缁勭殑姣忎竴椤逛細鏄竴琛 |
description | string | IntelliSense 鏄剧ず鐨勭墖娈电殑鍙夋弿杩 |
1鈭1 - 1鈭抧 | - | 瀹氫箟鍏夋爣鐨勪綅缃紝鍏夋爣鏍规嵁鏁板瓧澶у皬鎸塼ab渚濇璺宠浆锛涙敞鎰$0 鏄壒娈婂硷紝琛ㄧず鍏夋爣閫鍑虹殑浣嶇疆锛屾槸鏈鍚庣殑鍏夋爣浣嶇疆銆 |
- 鍦ㄩ敭鐩樿緭鍏
log
鏃舵晥鏋滃涓
- 鎸囧畾鍏夋爣澶勭殑榛樿鍊煎苟閫変腑
"body": [
"console.log('${1:abc}');"
],
澶嶅埗浠g爜
- 鎸囧畾鍏夋爣澶勭殑榛樿鍊兼湁澶氫釜锛屽苟鎻愪緵涓嬫媺閫夋嫨
鐢ㄤ袱涓珫绾垮寘鍚涓夋嫨鍊硷紝
|澶氫釜閫夋嫨鍊肩洿鎺ョ敤閫楀彿闅斿紑|
"body": [
"console.log('${1:abc}');",
"${2|aaa,bbb,ccc|}"
],
澶嶅埗浠g爜
鏂板缓褰撳墠宸ヤ綔鍖虹殑浠g爜鐗囨
鍙渶瑕侀夋嫨
鏂囦欢 \-> 棣栭夐」 \-> 鐢ㄦ埛鐗囨 \-> 鏂板缓xxx鏂囦欢澶圭殑浠g爜鐗囨
, 鏂板缓鍚庝細鍦ㄥ綋鍓嶅伐浣滃尯鐢熸垚.vscode/xxx.code-snippets
鏂囦欢鎶宸т簩锛欵mmet
vscode鍐呯疆浜嗗Emmet鐨勬敮鎸侊紝鏃犻渶棰濆鎵╁睍銆備緥濡俬tml鐨凟mmet婕旂ず濡備笅:
鎶宸т笁锛氬鍏夋爣澶勪唬鐮佸彉閲忓揩閫熼噸鍛藉悕
閫変腑鎴栬呭厜鏍囨墍澶勭殑浣嶇疆锛屾寜
F2
鍙互瀵规墍鏈夌殑鍙橀噺閲嶅懡鍚鎶宸у洓锛氫唬鐮侀噸鏋勫缓璁
- 閫変腑瑕侀噸鏋勭殑浠g爜锛岀偣鍑诲嚭鐜扮殑榛勮壊灏忕伅鐨勫浘鏍
- 閫変腑閲嶆瀯鐨勭被鍨
- 杈撳叆鏂扮殑鍙橀噺鍚
- 杩樺彲浠ラ噸鏋勫埌鍑芥暟
- TS涓繕鍙互鎻愬彇鎺ュ彛绛夌瓑
琛ュ厖锛歏sCode鎵╁睍鎻掍欢寮鍙
VsCode鎵╁睍鎻掍欢鍙互鍋氫粈涔堜簨鎯咃紵
- 瀹氬埗涓婚銆佹枃浠跺浘鏍
- 鎵╁睍宸ヤ綔鍙板姛鑳
- 鍒涘缓webView
- 鑷畾涔夋柊鐨勮瑷鎻愮ず
- 鏀寔璋冭瘯鐗瑰畾鐨剅untime
鍩轰簬
Yeoman
蹇熷紑鍙慥sCode鎻掍欢锛屾楠ゅ涓:- 瀹夎
Yeoman
鍜岀敤浜庣敓鎴愭ā鏉跨殑鎻掍欢VS Code Extension Generator[8]
# 缁堢杩愯锛屼富瑕乶ode鐗堟湰闇瑕12鍙婁互涓婏紝node10浼氬畨瑁呮姤閿
npm i -g yo generator-code
澶嶅埗浠g爜
- 杩愯
yo code
鍒涘缓鍛戒护锛岄夋嫨瑕佺敓鎴愮殑椤圭洰妯℃澘銆傝繖閲屾紨绀New extension
- 鏍规嵁鎻愮ず渚濇閫夋嫨
- 鐢熸垚鐨勫唴瀹瑰涓
- 鎸
F5
鐢熸垚缂栬瘧椤圭洰锛屾鏃朵細鑷姩鎵撳紑涓涓柊绐楀彛 - 鍦ㄦ柊绐楀彛鎸
Ctrl+Shfit+P
,杈撳叆Hello World
鍛戒护
- 姝ゆ椂浼氬脊鍑轰竴涓脊绐楃殑鏁堟灉
- 鑷虫锛屼竴涓渶绠鍗曠殑鎻掍欢灏卞畬鎴愪簡