VScode配置vue2用户代码片段

介绍

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<template>

  <div>



  </div>

</template>



<script>

export default {

    name: "component_name",

    data() {

        return {



        }

    },

    created() {



    },



    mounted() {



    },



    methods() {



    },



    destroyed() {



    }

}

</script>



<style scoped>



</style>

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

配置步骤

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

文件默认内容如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
{

    // 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 文件为例,配置模板内容如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
{

	"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 语法即可

使用 Hugo 构建
主题 StackJimmy 设计