使用 vscode 代码片段:vue snippets

本文介绍了如何在 Visual Studio Code (VSCode) 中配置和使用 Vue 代码片段。通过文件-首选项-用户片段创建 vue.json 文件,自定义Vue模板,然后利用快捷键快速插入代码片段,提高开发效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

vscode 代码片段配置

以创建 vue 文件模板为例——
文件-首选项-用户片段( 或使用快捷键 Ctrl + Shift + P,输入 snippets
在这里插入图片描述
输入 Vue,回车即可自动创建 vue.json 文件
在这里插入图片描述
在这里插入图片描述

配置成自己喜欢的 vue 模板,保存

// vue.json
{
	"Print to console": {
		"prefix": "vue",
		"body": [
			"<template>",
			" <div>\n",
			" </div>",
			"</template>\n",
			"<script>",
			"export default {",
			"  name: '',",
			"  props: { },",
			"  data () {",
			"    return { }",
			"  },",
			"  computed: { },",
			"  watch: { },",
			"  methods: { },",
			"  mounted() { }",
			"}",
			"</script>\n",
			"<style lang=\"scss\" scoped>",
			"  @import \"./index.scss\";",
			"</style>",
			"$2"
		],
		"description": "Log output to console"
	}
}

在这里插入图片描述

使用

使用快捷键 Ctrl + Shift + P,输入 insert snippet ,选中即可自动插入
在这里插入图片描述
在这里插入图片描述


同理,可配置其他代码片段

//egg 框架的 controller、 service 代码片段

{
	// Place your 全局 snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and 
	// description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope 
	// is left empty or omitted, the snippet gets applied to all languages. 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": {
	// 	"scope": "javascript,typescript",
	// 	"prefix": "log",
	// 	"body": [
	// 		"console.log('$1');",
	// 		"$2"
	// 	],
	// 	"description": "Log output to console"
	// }
	"egg-controller": {
		"scope": "javascript,typescript",
		"prefix": "egg controller",
		"body": [
			"'use strict';",
			"const Controller = require('egg').Controller;",
			"class $1Controller extends Controller {",
				"    async index() {",
					"        const { ctx } = this;",
					"        ctx.body = '$2';",
					"    }",
				"}",
				"module.exports = $1Controller;",
		],
		"description": "egg 控制器的代码提示"
	},
 
	"egg-service": {
		"scope": "javascript,typescript",
		"prefix": "egg service",
		"body": [
			"const Service = require('egg').Service;",
			"class $1Service extends Service {",
				"    async find(uid) {",
					"        const sql = ``;",
					"        const data = await this.app.mysql.query(sql);",
					"        return data;",
					"    }",
				"}",
				"module.exports = $1Service;",
		],
		"description": "egg 控制器的代码提示"
	}
} 

效果:
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值