在VScode中学习Vue框架时,如何快速生成属于自己的代码模板
我们在初始学习Vue的时候都会用到这样一个快速生成HTML代码的快捷键:!(注意是英文符号)+回车,即可生成以下代码:
<!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>
</body>
</html>
那么我们如何生成属于自己的代码片段与定制快捷键呢?
点击“设置”图标——选择“用户代码片段”
点击html.json
会出现这个界面
然后我们手动编辑你想要的代码
prefix后面是你定义的快捷键
body后面填写的是代码片段
以下为例:
{
// Place your snippets for html 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"
// }
"vue html":{
"prefix": "vh",
"body":[
"<!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>",
" <script src=\"vue.js\"></script>",
"</head>",
"<body>",
" <div id=\"app\"></div>",
" <script>",
" var vm = new Vue({",
" el:'#app',",
" data:{",
" },",
" methods:{",
" },",
" })",
" </script>",
"</body>",
"</html>",
]
}
}
注意每一行代码都有""包围,并且是逗号结尾。
如上面的代码,我设置的快捷键是vh,只要在文件里输入vh再按回车,即可快速得到代码片段:
此时按下回车
即可生成自己定制的代码模板。