目標:
我們希望每次新建.vue文件後,VSCODE能夠根據配置,自動生成我們想要的內容。
方法:
打開VSCODE編輯器,依次選擇“文件 -> 首選項 -> 用戶代碼片段”,此時,會彈出一個搜索框,我們輸入vue, 如下:
選擇vue後,VSCODE會自動打開一個名字爲vue.json的文件,複製以下內容到這個文件中:
{
// 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": "vue",
"body": [
"<!-- $0 -->",
"<template>",
" <div></div>",
"</template>",
"",
"<script>",
"export default {",
" data () {",
" return {",
" }",
" },",
"",
" components: {},",
"",
" computed: {},",
"",
" methods: {},",
"",
" mounted: {},",
"",
" created: {},",
"}",
"</script>",
"",
"<style scoped>",
"",
"</style>"
],
"description": "vue 模板"
}
}
保存後關閉這個文件。
稍稍解釋一下:
$0 表示你希望生成代碼後光標的位置 ; prefix 表示生成對應預設代碼的命令
例如:我們新建一個名爲header.vue的文件,輸入vue按下enter,就會自動生成內容(此處應該有截圖):
如下圖:
後記
當然,你也可以爲.js、.html等各種文件預設代碼片段,具體設置可以搜索一下snippet,瞭解用法。
這裏推薦一個snippet用法結束的網址:
https://blog.csdn.net/maokelong95/article/details/54379046/