使用VSCode的代码片段提高效率

木头的喵喵拖孩

工作中有很多重复的代码,使用 VSCode 的代码片段可以快速实现重复代码的编写,提高效率。

这里放几个工作中经常用到的代码片段:

vue2 和 scss 初始页面代码片段

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{
"vue2的scss页面": {
"scope": "vue",
"prefix": "vue-scss",
"body": [
"<template>",
"\t<div class=\"component-name\"></div>",
"</template>",
"",
"<script>",
"export default {",
"\tdata: () => ({}),",
"};",
"</script>",
"",
"<style scoped lang=\"scss\">",
".component-name {",
"}",
"</style>"
],
"description": "在vue2下,使用scss创建一个初始页面"
}
}

vue2 下 ElementUI 的 el-table 初始页面代码片段

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
{
"vue2的el-table页面": {
"scope": "vue",
"prefix": "vue-el-table",
"body": [
"<template>",
"\t<div class=\"component-name\">",
"\t\t<el-table :data=\"tableData\" height=\"100%\">",
"\t\t\t<el-table-column",
"\t\t\t\tv-for=\"column in columns\"",
"\t\t\t\t:key=\"column.prop\"",
"\t\t\t\t:label=\"column.label\"",
"\t\t\t>",
"\t\t\t\t<template slot-scope=\"scope\">",
"\t\t\t\t\t<template v-if=\"column.prop === 'actions'\">",
"\t\t\t\t\t\t<el-button>按钮</el-button>",
"\t\t\t\t\t</template>",
"\t\t\t\t\t<template v-else>",
"\t\t\t\t\t\t{{ scope.row[column.prop] }}",
"\t\t\t\t\t</template>",
"\t\t\t\t</template>",
"\t\t\t</el-table-column>",
"\t\t</el-table>",
"\t</div>",
"</template>",
"",
"<script>",
"export default {",
"\tdata: () => ({",
"\t\tcolumns: [",
"\t\t\t{ prop: \"xxxxx1\", label: \"列1\" },",
"\t\t\t{ prop: \"actions\", label: \"操作\" },",
"\t\t],",
"\t\ttableData: [{ xxxxx1: \"值1\" }],",
"\t}),",
"};",
"</script>",
"",
"<style scoped lang=\"scss\">",
".component-name {",
"}",
"</style>"
],
"description": "在vue2下,使用elementUI的el-table创建一个初始页面"
}
}

vue2 下 ElementUI 的 el-dialog 初始页面代码片段

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
{
"vue2的el-dialog页面": {
"scope": "vue",
"prefix": "vue-el-dialog",
"body": [
"<template>",
"\t<el-dialog",
"\t\t:visible.sync=\"_visible\"",
"\t\t:title=\"title\"",
"\t\tcustom-class=\"component-name\"",
"\t\twidth=\"35vw\"",
"\t\t:modal=\"true\"",
"\t\t:close-on-click-modal=\"true\"",
"\t\t:close-on-press-escape=\"true\"",
"\t\t:append-to-body=\"false\"",
"\t>",
"\t\t<div class=\"test\">弹窗内容</div>",
"\t\t<span slot=\"footer\">",
"\t\t\t<el-button @click=\"_visible = false\">取消</el-button>",
"\t\t\t<el-button type=\"primary\" @click=\"_visible = false\">确定</el-button>",
"\t\t</span>",
"\t</el-dialog>",
"</template>",
"\t",
"\t<script>",
"export default {",
"\tprops: {",
"\t\tvisible: {",
"\t\t\ttype: Boolean,",
"\t\t\tdefault: false,",
"\t\t},",
"\t},",
"\tdata: () => ({",
"\t\ttitle: \"弹窗标题\",",
"\t}),",
"\tcomputed: {",
"\t\t_visible: {",
"\t\t\tget() {",
"\t\t\t\treturn this.visible;",
"\t\t\t},",
"\t\t\tset(newVal) {",
"\t\t\t\tthis.$$emit(\"update:visible\", newVal);",
"\t\t\t},",
"\t\t},",
"\t},",
"};",
"</script>",
"\t",
"\t<style scoped lang=\"scss\">",
".component-name {",
"\t> .el-dialog__header {",
"\t}",
"\t> .el-dialog__body {",
"\t\t> .test {",
"\t\t}",
"\t}",
"}",
"</style>"
],
"description": "在vue2下,使用elementUI的el-dialog创建一个初始页面"
}
}

vue2 下 ElementUI 的 el-form 初始页面代码片段

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
{
"vue2的el-form页面": {
"scope": "vue",
"prefix": "vue-el-form",
"body": [
"<template>",
"\t<div class=\"component-name\">",
"\t\t<el-form",
"\t\t\tref=\"fuleForm\"",
"\t\t\t:model=\"formData\"",
"\t\t\tlabel-position=\"top\"",
"\t\t\t:rules=\"formRules\"",
"\t\t>",
"\t\t\t<el-form-item prop=\"xxxxx1\" label=\"文本框\">",
"\t\t\t\t<el-input v-model=\"formData.xxxxx1\" clearable></el-input>",
"\t\t\t</el-form-item>",
"\t\t\t<el-form-item prop=\"xxxxx2\" label=\"下拉框\">",
"\t\t\t\t<el-select",
"\t\t\t\t\tv-model=\"formData.xxxxx2\"",
"\t\t\t\t\tplaceholder=\"请选择下拉框\"",
"\t\t\t\t\t:multiple=\"false\"",
"\t\t\t\t\tclearable",
"\t\t\t\t>",
"\t\t\t\t\t<el-option",
"\t\t\t\t\t\tv-for=\"item in selectItems\"",
"\t\t\t\t\t\t:key=\"item.value\"",
"\t\t\t\t\t\t:label=\"item.label\"",
"\t\t\t\t\t\t:value=\"item.value\"",
"\t\t\t\t\t></el-option>",
"\t\t\t\t</el-select>",
"\t\t\t</el-form-item>",
"\t\t\t<el-form-item prop=\"xxxxx3\" label=\"级联下拉框\">",
"\t\t\t\t<el-cascader",
"\t\t\t\t\tv-model=\"formData.xxxxx3\"",
"\t\t\t\t\t:props=\"cascProps\"",
"\t\t\t\t\tplaceholder=\"请选择级联下拉框\"",
"\t\t\t\t\tclearable",
"\t\t\t\t></el-cascader>",
"\t\t\t</el-form-item>",
"\t\t\t<el-form-item prop=\"xxxxx4\" label=\"日期\">",
"\t\t\t\t<el-date-picker",
"\t\t\t\t\tv-model=\"formData.xxxxx4\"",
"\t\t\t\t\ttype=\"date\"",
"\t\t\t\t\tplaceholder=\"选择日期\"",
"\t\t\t\t\tformat=\"yyyy-MM-dd\"",
"\t\t\t\t\tvalue-format=\"yyyy-MM-dd\"",
"\t\t\t\t\tclearable",
"\t\t\t\t></el-date-picker>",
"\t\t\t</el-form-item>",
"\t\t\t<el-form-item prop=\"xxxxx5\" label=\"日期范围\">",
"\t\t\t\t<el-date-picker",
"\t\t\t\t\tv-model=\"formData.xxxxx5\"",
"\t\t\t\t\ttype=\"daterange\"",
"\t\t\t\t\trange-separator=\"至\"",
"\t\t\t\t\tstart-placeholder=\"开始日期\"",
"\t\t\t\t\tend-placeholder=\"结束日期\"",
"\t\t\t\t\tplaceholder=\"选择日期范围\"",
"\t\t\t\t\tformat=\"yyyy-MM-dd\"",
"\t\t\t\t\tvalue-format=\"yyyy-MM-dd\"",
"\t\t\t\t\tclearable",
"\t\t\t\t></el-date-picker>",
"\t\t\t</el-form-item>",
"\t\t\t<el-form-item prop=\"xxxxx6\" label=\"时间\">",
"\t\t\t\t<el-time-picker",
"\t\t\t\t\tv-model=\"formData.xxxxx6\"",
"\t\t\t\t\tplaceholder=\"选择时间\"",
"\t\t\t\t\tformat=\"HH:mm:ss\"",
"\t\t\t\t\tvalue-format=\"HH:mm:ss\"",
"\t\t\t\t\tclearable",
"\t\t\t\t></el-time-picker>",
"\t\t\t</el-form-item>",
"\t\t\t<el-form-item prop=\"xxxxx7\" label=\"时间范围\">",
"\t\t\t\t<el-time-picker",
"\t\t\t\t\tv-model=\"formData.xxxxx7\"",
"\t\t\t\t\tis-range",
"\t\t\t\t\trange-separator=\"至\"",
"\t\t\t\t\tstart-placeholder=\"开始时间\"",
"\t\t\t\t\tend-placeholder=\"结束时间\"",
"\t\t\t\t\tplaceholder=\"选择时间范围\"",
"\t\t\t\t\tformat=\"HH:mm:ss\"",
"\t\t\t\t\tvalue-format=\"HH:mm:ss\"",
"\t\t\t\t\tclearable",
"\t\t\t\t></el-time-picker>",
"\t\t\t</el-form-item>",
"\t\t\t<el-form-item prop=\"xxxxx8\" label=\"日期时间\">",
"\t\t\t\t<el-date-picker",
"\t\t\t\t\tv-model=\"formData.xxxxx8\"",
"\t\t\t\t\ttype=\"datetime\"",
"\t\t\t\t\tplaceholder=\"选择日期时间\"",
"\t\t\t\t\tformat=\"yyyy-MM-dd HH:mm:ss\"",
"\t\t\t\t\tvalue-format=\"yyyy-MM-dd HH:mm:ss\"",
"\t\t\t\t\tclearable",
"\t\t\t\t>",
"\t\t\t\t</el-date-picker>",
"\t\t\t</el-form-item>",
"\t\t\t<el-form-item prop=\"xxxxx9\" label=\"日期时间范围\">",
"\t\t\t\t<el-date-picker",
"\t\t\t\t\tv-model=\"formData.xxxxx9\"",
"\t\t\t\t\ttype=\"datetimerange\"",
"\t\t\t\t\tplaceholder=\"选择日期时间范围\"",
"\t\t\t\t\trange-separator=\"至\"",
"\t\t\t\t\tstart-placeholder=\"开始日期\"",
"\t\t\t\t\tend-placeholder=\"结束日期\"",
"\t\t\t\t\tformat=\"yyyy-MM-dd HH:mm:ss\"",
"\t\t\t\t\tvalue-format=\"yyyy-MM-dd HH:mm:ss\"",
"\t\t\t\t\tclearable",
"\t\t\t\t>",
"\t\t\t\t</el-date-picker>",
"\t\t\t</el-form-item>",
"\t\t\t<el-form-item label=\"开关\">",
"\t\t\t\t<el-switch",
"\t\t\t\t\tv-model=\"formData.xxxxx10\"",
"\t\t\t\t\tactive-color=\"green\"",
"\t\t\t\t\tinactive-color=\"red\"",
"\t\t\t\t\tactive-text=\"开启\"",
"\t\t\t\t\tinactive-text=\"关闭\"",
"\t\t\t\t></el-switch>",
"\t\t\t</el-form-item>",
"\t\t\t<el-form-item prop=\"xxxxx11\" label=\"多选框\">",
"\t\t\t\t<el-checkbox-group v-model=\"formData.xxxxx11\">",
"\t\t\t\t\t<el-checkbox",
"\t\t\t\t\t\tv-for=\"item in checkboxItems\"",
"\t\t\t\t\t\t:key=\"item.value\"",
"\t\t\t\t\t\t:label=\"item.value\"",
"\t\t\t\t\t\t>{{ item.label }}</el-checkbox",
"\t\t\t\t\t>",
"\t\t\t\t</el-checkbox-group>",
"\t\t\t</el-form-item>",
"\t\t\t<el-form-item prop=\"xxxxx12\" label=\"单选框\">",
"\t\t\t\t<el-radio-group v-model=\"formData.xxxxx12\">",
"\t\t\t\t\t<el-radio",
"\t\t\t\t\t\tv-for=\"item in radioItems\"",
"\t\t\t\t\t\t:key=\"item.value\"",
"\t\t\t\t\t\t:label=\"item.value\"",
"\t\t\t\t\t\t>{{ item.label }}</el-radio",
"\t\t\t\t\t>",
"\t\t\t\t</el-radio-group>",
"\t\t\t</el-form-item>",
"\t\t\t<el-form-item prop=\"xxxxx13\" label=\"文本域\">",
"\t\t\t\t<el-input",
"\t\t\t\t\tv-model=\"formData.xxxxx13\"",
"\t\t\t\t\ttype=\"textarea\"",
"\t\t\t\t\tplaceholder=\"请输入内容\"",
"\t\t\t\t\t:autosize=\"{ minRows: 2, maxRows: 4 }\"",
"\t\t\t\t\tmaxlength=\"10\"",
"\t\t\t\t\tclearable",
"\t\t\t\t></el-input>",
"\t\t\t</el-form-item>",
"\t\t\t<div>",
"\t\t\t\t<el-button type=\"primary\" @click=\"onSubmit('fuleForm')\"",
"\t\t\t\t\t>提交</el-button",
"\t\t\t\t>",
"\t\t\t\t<el-button>取消</el-button>",
"\t\t\t</div>",
"\t\t</el-form>",
"\t</div>",
"</template>",
"",
"<script>",
"export default {",
"\tdata: () => ({",
"\t\tselectItems: [",
"\t\t\t{ label: \"区域一\", value: \"shanghai\" },",
"\t\t\t{ label: \"区域二\", value: \"beijing\" },",
"\t\t],",
"\t\tcascItms: [",
"\t\t\t{",
"\t\t\t\tvalue: \"zhinan\",",
"\t\t\t\tlabel: \"指南\",",
"\t\t\t},",
"\t\t],",
"\t\tcheckboxItems: [",
"\t\t\t{ label: \"美食/餐厅线上活动\", value: 1 },",
"\t\t\t{ label: \"地推活动\", value: 2 },",
"\t\t\t{ label: \"线下主题活动\", value: 3 },",
"\t\t\t{ label: \"单纯品牌曝光\", value: 4 },",
"\t\t],",
"\t\tradioItems: [",
"\t\t\t{ label: \"线上品牌商赞助\", value: 1 },",
"\t\t\t{ label: \"线下场地免费\", value: 2 },",
"\t\t],",
"\t\tformRules: {",
"\t\t\txxxxx1: { required: true, message: \"请输入内容\", trigger: \"blur\" },",
"\t\t\txxxxx2: {",
"\t\t\t\trequired: true,",
"\t\t\t\tmessage: \"请选择内容\",",
"\t\t\t\ttrigger: \"change\",",
"\t\t\t},",
"\t\t\txxxxx3: {",
"\t\t\t\trequired: true,",
"\t\t\t\tmessage: \"请选择内容\",",
"\t\t\t\ttrigger: \"change\",",
"\t\t\t},",
"\t\t\txxxxx4: {",
"\t\t\t\trequired: true,",
"\t\t\t\tmessage: \"请选择日期\",",
"\t\t\t\ttrigger: \"change\",",
"\t\t\t},",
"\t\t\txxxxx5: {",
"\t\t\t\trequired: true,",
"\t\t\t\tmessage: \"请选择日期范围\",",
"\t\t\t\ttrigger: \"change\",",
"\t\t\t},",
"\t\t\txxxxx6: {",
"\t\t\t\trequired: true,",
"\t\t\t\tmessage: \"请选择时间\",",
"\t\t\t\ttrigger: \"change\",",
"\t\t\t},",
"\t\t\txxxxx7: {",
"\t\t\t\trequired: true,",
"\t\t\t\tmessage: \"请选择时间范围\",",
"\t\t\t\ttrigger: \"change\",",
"\t\t\t},",
"\t\t\txxxxx8: {",
"\t\t\t\trequired: true,",
"\t\t\t\tmessage: \"请选择日期时间\",",
"\t\t\t\ttrigger: \"change\",",
"\t\t\t},",
"\t\t\txxxxx9: {",
"\t\t\t\trequired: true,",
"\t\t\t\tmessage: \"请选择日期时间范围\",",
"\t\t\t\ttrigger: \"change\",",
"\t\t\t},",
"\t\t\txxxxx11: {",
"\t\t\t\trequired: true,",
"\t\t\t\tmessage: \"请选择多选框\",",
"\t\t\t\ttrigger: \"change\",",
"\t\t\t},",
"\t\t\txxxxx12: {",
"\t\t\t\trequired: true,",
"\t\t\t\tmessage: \"请选择单选框\",",
"\t\t\t\ttrigger: \"change\",",
"\t\t\t},",
"\t\t\txxxxx13: { required: true, message: \"请输入内容\", trigger: \"blur\" },",
"\t\t},",
"\t\tformData: {",
"\t\t\txxxxx1: undefined,",
"\t\t\txxxxx2: undefined,",
"\t\t\txxxxx3: undefined,",
"\t\t\txxxxx4: undefined,",
"\t\t\txxxxx5: [],",
"\t\t\txxxxx6: undefined,",
"\t\t\txxxxx7: [],",
"\t\t\txxxxx8: undefined,",
"\t\t\txxxxx9: [],",
"\t\t\txxxxx10: false,",
"\t\t\txxxxx11: [],",
"\t\t\txxxxx12: undefined,",
"\t\t\txxxxx13: undefined,",
"\t\t},",
"\t}),",
"\tcomputed: {",
"\t\tcascProps() {",
"\t\t\treturn {",
"\t\t\t\tmultiple: false,",
"\t\t\t\tlazy: true,",
"\t\t\t\tlazyLoad: this.cascLazyLoad, // 此处如果要引用this的方法,则不能将cascProps放到data里,而是需要将cascProps设置为计算属性",
"\t\t\t};",
"\t\t},",
"\t},",
"\tmethods: {",
"\t\tcascLazyLoad(node, resolve) {",
"\t\t\tlet { level } = node; // level从0开始",
"\t\t\tsetTimeout(() => {",
"\t\t\t\tlet result = this.cascItms.map((v) => ({",
"\t\t\t\t\t...v,",
"\t\t\t\t\tleaf: level >= 2, // 叶子节点,代表最终结果,不会再异步查询",
"\t\t\t\t}));",
"\t\t\t\tresolve(result);",
"\t\t\t}, 500);",
"\t\t},",
"\t\tonSubmit(formName) {",
"\t\t\tthis.$$refs[formName].validate((valid) => {",
"\t\t\t\tif (valid) {",
"\t\t\t\t\tconsole.log(this.formData);",
"\t\t\t\t} else {",
"\t\t\t\t\treturn false;",
"\t\t\t\t}",
"\t\t\t});",
"\t\t},",
"\t},",
"};",
"</script>",
"",
"<style scoped lang=\"scss\">",
".component-name {",
"\t> .el-form {",
"\t\t> .el-form-item {",
"\t\t\t/deep/ > .el-form-item__content {",
"\t\t\t\t> * {",
"\t\t\t\t\twidth: 100%;",
"\t\t\t\t}",
"\t\t\t}",
"\t\t}",
"\t}",
"}",
"</style>"
],
"description": "在vue2下,使用elementUI的el-form创建一个初始页面"
}
}
  • 标题: 使用VSCode的代码片段提高效率
  • 作者: 木头的喵喵拖孩
  • 创建于: 2024-09-27 17:16:39
  • 更新于: 2024-10-09 15:52:22
  • 链接: https://blog.xx-xx.top/2024/09/27/使用VSCode的代码片段提高效率/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。