【VSCode】文件模板创建及使用.md

背景

最近使用VSCode学习Vue项目比较频繁,每次创建Vue文件都要手动写重复代码,特别麻烦,就上网查找自动生成代码的说明,结果发现VSCode有代码模板,怪怪,感觉发现新大陆了(low!)。

配置

  1. 打开配置

    • 方式一 :首先打开File->Preferences->Configure User Snippets配置(文件->首选项->配置用户片段):
    • 方式二 :或通过快捷键Ctrl+Shift+P打开命令面板,输入snippets,选择Configure User Snippets
  2. 然后选择需要配置模板的对应文件类型(以vue模板为例,选择vue.json,其他模板同理为类型.json),如:

  3. 配置自定义代码模板:

    • "Print to console" 代码模板内容
    • "prefix" 代码模板前缀,在对应类型中使用该前缀可触发根据代码模板生成代码
    • "body" 代码模板数据,实际是字符串数组
    • $1,$2,$3 代码模板变量,根据变量顺序从1开始,$0为最后一个变量,会在代码生成后输入,根据次序代表变量顺序
    • ${1:default} 代码模板变量默认值,当不输入时以默认值显示,否则显示输入值
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": "log",
	// 	"body": [
	// 		"console.log('$1');",
	// 		"$2"
	// 	],
	// 	"description": "Log output to console"
	// }
    "Print to console":{
		"prefix": "vue",
		"body": [
			"<template>",
			"	<div class='${1:home}'></div>",
			"</template>",
			"<script>",
			"export default {",
			"	name: '${2:Home}',",
			"	data() {",
			"		return {",
			"		}",
			"	},",
			"	methods: {},",
			"};",
			"</script>",
		]
	}
}

使用

  1. 配置完成后,在VSCode中输入模板名称,按tab键即可生成模板代码,如下图所示:

相关推荐
蚂蚁不吃土&13 小时前
vscode + chinese(simplified)
ide·vscode·编辑器
Cloud_Shy61818 小时前
Python 数据分析基础入门:《Excel Python:飞速搞定数据分析与处理》学习笔记系列(附录 B 高级 VS Code 功能)
vscode·python·jupyter·数据分析·excel
唐璜Taro1 天前
VSCode接入小米mimo大模型
ide·vscode·编辑器
2501_916008891 天前
ChatGPT前端开发学习指南:Visual Studio Code与谷歌浏览器安装配置详解
ide·vscode·ios·小程序·uni-app·编辑器·iphone
视觉小萌新1 天前
Ubuntu-Liunx下进行Vscode的C++编程——hello world_x64版本
ide·vscode·编辑器
北暮城南1 天前
VS Code 与 IDEA 集成 Claude Code 实战指南——基于智谱 AI 大模型的 AI 辅助编码环境搭建
vscode·idea·claude·intellij idea·claude code·claude code cli
AC梦1 天前
在Claude Code中接入Deepseek-v4模型
vscode·ai
00后程序员张1 天前
iOS开发中Xcode安装不完整问题解决方案与配置指南
ide·vscode·ios·objective-c·个人开发·swift·敏捷流程
安大小万2 天前
VSCode的json文件不允许注释的解决办法
ide·vscode·编辑器
拉不拉斯2 天前
vsCode远程连接Centos 7出现的错误以及解决办法
ide·vscode·centos