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.
In VSCode, open a folder that will contain your new project. Use the Command Palette to execute the command "Project: Create Project From Template". A list of available templates should appear. Select the desired template.
Templates can be accessed through search (Ctrl+Q) and in the “New Project Dialog” (Ctrl+Shift+N). Both entry points will also be enabled with fuzzy search (to help automatically rectify typos), highlighted matches to your search query in the results, and improved ranking to ensure increased accuracy.
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.
The extension Auto Snippet does exactly that.
You only need to configure two things:
The author has some very custom defaults, so as soon as you install it, modify its settings and remove those patterns that you won't need.
Otherwise, it will complain every time you create a file and doesn't find the snippet configured.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With