VScode配置vue2用户代码片段
如默
撰写于 2019年 07月 25 日

介绍

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

<template>
  <div>

  </div>
</template>

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

        }
    },
    created() {

    },

    mounted() {

    },

    methods() {

    },

    destroyed() {

    }
}
</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": "vue2",
        "body": [
            "<template>",
            "  <div>\n$0",
            "  </div>",
            "</template>\n",
            "<script>",
            "export default {",
            "    name: \"${1:component_name}\",",
            "    data() {",
            "        return {\n",
            "        }",
            "    },",
            "    created() {\n",
            "    },\n",
            "    mounted() {\n",
            "    },\n",
            "    methods() {\n",
            "    },\n",
            "    destroyed() {\n",
            "    }",
            "}",
            "</script>\n",
            "<style scoped>\n",
            "</style>"
        ],
        "description": "default vue template"
    }
}

注意:模板里不能使用tab制表符,否则会报错

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

其他格式代码参考 vue 语法即可

VScode配置vue2用户代码片段

温馨提示:

本文最后更新于2019年07月25日,已超过2037天没有更新,若内容或图片失效,请留言反馈。

介绍

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

<template>
  <div>

  </div>
</template>

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

        }
    },
    created() {

    },

    mounted() {

    },

    methods() {

    },

    destroyed() {

    }
}
</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": "vue2",
        "body": [
            "<template>",
            "  <div>\n$0",
            "  </div>",
            "</template>\n",
            "<script>",
            "export default {",
            "    name: \"${1:component_name}\",",
            "    data() {",
            "        return {\n",
            "        }",
            "    },",
            "    created() {\n",
            "    },\n",
            "    mounted() {\n",
            "    },\n",
            "    methods() {\n",
            "    },\n",
            "    destroyed() {\n",
            "    }",
            "}",
            "</script>\n",
            "<style scoped>\n",
            "</style>"
        ],
        "description": "default vue template"
    }
}

注意:模板里不能使用tab制表符,否则会报错

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

其他格式代码参考 vue 语法即可


赞 (0)

猜您想看

  • Visual Studio Code插件koro1FileHeader使用记录

    VS code是一个非常方便的工具,加上免费,所以一直在使用。 本文记录一下此插件的配置过程以及配置文件,方便以后使用。

    2019年05月15日
  • 关于日常家用镜头的选择,RF 24-105 f7.1和f4和 RF 70-200 f4和f2.8

    前两天买了EOS RP和套机头 RF 24-105 f7.1,这两个组合其实很完美,不得不说佳能刀法精准,这个入门的全画幅,搭配入门的这个STM镜头十分合适,但想收入其他的镜头,RF 24-105 f4或者 RF 70-200的两款白炮,在此说说看法。

    2022年10月25日
  • Magic Trackpad使用体验(Windows11 和 Mac OS13.2)

    如题,我购买了apple的Magic Trackpad和一台Mac Mini,但是后者预计发货时间在月底,可秒控板首先到了,没有Mac,不能测试触控板吗?幸好,我在全球最大的同性交友网站上找到了相关驱动,记录一下。

    2023年02月19日
  • PotPlayer使用电视直播源

    由于央视影音软件有很多的广告,也经常遇到各种bug,加上最近发现`PotPlayer`可以看斗鱼等直播,遂找一些电视直播源进行观看,特此记录。

    2020年11月22日
  • Android Studio设置国内镜像源

    没有科学上网工具,国内无法很好的访问Android资源,特记录国内镜像配置过程。

    2019年10月17日
  • Microsoft Edge Chromium版使用体验

    早前,微软发布了基于Chromium内核的新版edge浏览器,使用三个月来,体验良好,特记录如下。

    2019年12月11日

评论区(暂无评论)

这里空空如也,快来评论吧~

我要评论

Vaptcha 初始化中...