最佳答案
您可以使用“重构...”快捷键 Ctrl+Shift+R 将表达式提取为常量。示例:
function main(){
return "foo".replace("o", "a")
}
标记表达式 "foo".replace("o", "a")
或整行→“Extract toconstant in enclosure scope”,将其命名为 test
并且该函数将被重构为以下内容:
function main(){
const test = "foo".replace("o", "a")
return test
}
还有用于修复和重构的“快速修复”命令 (Ctrl+.)。
<小时/>如果您想要此操作的额外快捷方式,请在 keybindings.json ( docs ) 中手动定义它:
{
"key": "shift+ctrl+alt+r",
"command": "editor.action.codeAction",
"args": {
"kind": "refactor.extract.constant"
}
}
关于javascript - 在 Visual Studio Code 中提取 JS/TS 中的局部变量的键盘快捷键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60360931/