如何在 Visual Studio Code 中为文件扩展名创建模板? [英] How can I create templates for file extensions in Visual Studio Code?
问题描述
在我的 Vue.js 项目中几乎所有时间我都需要这个代码片段作为模板.
<div>
<模板><脚本>导出默认{数据(){返回{}},方法:{},创建(){}}<样式范围></风格>
每次我创建扩展名为 .vue 的文件时,有没有办法告诉 Visual Studio Code 自动将该代码段添加到文件中?
简单地说,当我创建具有特定扩展名的新文件时,该扩展名的预定义模板应自动添加到文件中.
没有,不是原生的.但是有一个名为
当然,您也可以使用代码段生成器来制作自己的代码段.
In my Vue.js projects almost all the times I need this code snippet as a template.
<template>
<div>
</div>
<template>
<script>
export default{
data(){
return{
}
},
methods:{
},
created(){
}
}
</script>
<style scoped>
</style>
Is there a way to tell Visual Studio Code each and every time I create a file with the extension .vue to automatically add that snippet to the file?
Simply, when I create new file with a certain extension, the predefined template for that extension should automatically be added to the file.
There isn't, not natively. But there is an extension called File Templates for VSCode that allows you to create your own file templates and generate from them. But I think you'd benefit from making an extension to do just that and maybe even more.
In the meantime, you can use a snippet to generate this instead of having to copy paste.
Go to File > Preferences > User Snippets and choose Vue from the dropdown. Vue will only show up if you have installed an extension that supports this file type. In this case, I'd recommend Vetur, but you probably have it already.
Then just add this entry to your vue.json
file:
"vuetpl" : {
"body": [
"<template>",
"\t<div>",
"\t\t$0",
"\t</div>",
"</template>",
"<script>",
"export default{",
"\tdata(){",
"\t\treturn{",
"\t\t\t",
"\t\t}",
"\t},",
"\tmethods:{",
"\t\t",
"\t},",
"\tcreated(){",
"\t\t",
"\t}",
"}",
"</script>",
"<style scoped>",
"</style>",
],
"prefix": "vuetpl",
"description": "Creates a new template."
}
Then, when you create a new .vue
file, just type vuetpl
and press tab
to autocomplete, and you'll have this:
Of course, you can also use a Snippet Generator to make your own snippets.
这篇关于如何在 Visual Studio Code 中为文件扩展名创建模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!