VSCode设置代码块(Snippets),快捷初始化vue模板

本文用作记录设置Vue Snippets,快捷初始化vue模板(适用于Mac版VSCode,Windows待尝试)。

一、使用User Snippet

Code -> Preference -> User Snippets -> New (global) Snippets (可选当前文件和全局配置),复制以下配置,保存。
使用:”prefix“值为快捷方式,适用.vue后缀的文件。以下为用作初始化vue模板的例子,也可根据此格式自定义代码块。

{
	"Print to console": {
		"prefix": "vue",
		"body": [
			"<!-- $1 -->",
			"<template>",
			"<div class='$2'>$5</div>",
			"</template>",
			"",
			"<script>",
			"// 这里可以导入其他文件(比如:组件,工具js,第三方插件js,json文件,图片文件等等)",
			"// 例如:import 《组件名称》 from '《组件路径》';",
			"",
			"export default {",
			"// import引入的组件需要注入到对象中才能使用",
			"  components: {},",
			"  data () {",
			"  // 这里存放数据",
			"    return {",
			"",
			"    }",
			"  },",
			"  // 监听属性 类似于data概念",
			"  computed: {},",
			"  // 监控data中的数据变化",
			"  watch: {},",
			"  // 方法集合",
			"  methods: {",
			"",
			"  },",
			"  // 生命周期 - 创建完成(可以访问当前this实例)",
			"  created () {",
			"",
			"  },",
			"  // 生命周期 - 挂载完成(可以访问DOM元素)",
			"  mounted () {",
			"",
			"  },",
			"  beforeCreate () {}, // 生命周期 - 创建之前",
			"  beforeMount () {}, // 生命周期 - 挂载之前",
			"  beforeUpdate () {}, // 生命周期 - 更新之前",
			"  updated () {}, // 生命周期 - 更新之后",
			"  beforeDestroy () {}, // 生命周期 - 销毁之前",
			"  destroyed () {}, // 生命周期 - 销毁完成",
			"  activated () {} // 如果页面有keep-alive缓存功能,这个函数会触发",
			"}",
			"</script>",
			"<style lang='scss' scoped>",
			"// @import url($3); 引入公共css类",
			"$4",
			"</style>"
			""
		],
		"description": "Log output to console"
	}
	//  Place your global 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"
	//  }
}

二、使用VSCode插件

快捷初始化vue模板,推荐使用此方法。缺点:不支持自定义代码块,只可用做初始化vue模板。
安装VSCode插件:vue init biaov。vue文件中输入‘init’,回车即可。

参考:https://blog.csdn.net/lei_0912/article/details/98867087 (侵删)


版权声明:本文为weixin_38252066原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。