如默

VScode新建文件初始化模板
介绍如题,使用VScode新建一个文件的时候,比如 vue 文件,我们需要手动输入以下内容:
扫描右侧二维码阅读全文
20
2019/07

VScode新建文件初始化模板

介绍

如题,使用VScode新建一个文件的时候,比如 vue 文件,我们需要手动输入以下内容:

<template>
 
</template>

<script>
export default {
  name: "",
  data() {
    return {

    }
  }
}
</script>

<style scoped>

</style>

每次新建都需要这么做,显得很麻烦,所以,我们需要配置一个模板

配置步骤

点击顶部菜单,文件-首选项-用户代码片段,
选择新建全局代码片段文件,随便输入一个文件名,比如vue

文件默认内容如下:

{
    // 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"
    // }
}

格式说明

配置文件需要三部分,prefix就是触发此模板文件的命令,body是模板内容,description就是描述信息

配置内容

以 vue 文件为例,配置模板内容如下:

"Create vue template": {
        "prefix": "vue",
        "body": [
            "<template>",
            "  <div>\n$0",
            "  </div>",
            "</template>\n",
            "<script>",
            "export default {",
            "  name: \"${1:component_name}\",",
            "  data() {",
            "    return {\n",
            "    }",
            "  }",
            "}",
            "</script>\n",
            "<style scoped>\n",
            "</style>"
        ],
        "description": "default vue template"
    }

将上述代码复制到{}之内即可

此时,新建一个 vue 文件,输入vue,按回车,会自动创建模板

其他格式代码参考 vue 即可

Last modification:July 20th, 2019 at 06:01 pm
^_^您的每一分钱,都是对我最大的鼓励!

Leave a Comment