写一个VSCode snippets 代码片段

在上篇文章中(基于element-ui 搭建管理后台_夜跑者的博客-CSDN博客_elementui搭建后台)

我们基于element-ui搭建了一个通用的管理后台,在网络模块,对接口进行了封装,get, post请求如下所示:

    this.$http
      .get({ urlKey: "iot-getAsset", params: {} })
      .then((res) => {
        console.log("iot-getAsset res is: ", res);
      })
      .catch((err) => {
        console.error("iot-getAsset err is: ", err);
      });


    this.$http
      .post({ urlKey: "iot-getAsset", data: {} })
      .then((res) => {
        console.log("iot-getAsset res is: ", res);
      })
      .catch((err) => {
        console.error("iot-getAsset err is: ", err);
      });

这是两段通用网络接口代码,每个请求都这么写。我们能不能做个小工具方便的输入这些通用的代码片段呢?当然可以的。有经验的前端工程师基本上都安装了类似这样的VSCode插件。所以我们自定义的这个通用代码片段当然可以用VSCode插件来实现。为这2段代码片段写个VSCode插件有点杀鸡焉用牛刀的感觉。有没有更轻量的实现方案呢?VSCode 提供了snippets来实现。

1. 创建snippets文件

VSCode 编辑器 File -> Preferences -> Configure User Snippets   回车  选择New Snippets

2. 书写代码片段

{
	// 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"
	// }
	"http get shortcut": {
		"scope": "javascript, typescript",
		"prefix": "vhttpget",
		"body": ["this.\\$http.get({urlKey: '$1', params: {}}).then(res => {console.log('$1 res is: ', res)}).catch(err => {console.error('$1 err is: ', err)})"],
		"description": "a http get short cut"
	},
	"http post shortcut": {
		"scope": "javascript, typescript",
		"prefix": "vhttpost",
		"body": ["this.\\$http.post({urlKey: '$1', data: {}}).then(res => {console.log('$1 res is: ', res)}).catch(err => {console.error('$1 err is: ', err)})"],
		"description": "a http post short cut"
	},
}

按注释进行书写代码片段就可以了,语法请参考:Snippets in Visual Studio Code

我们在.vue 中的<script>标签下输入vhttpget 就会直接输入代码片段如下所示:

 再shift+alt + f 进行格式化代码就可以啦。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在VSCode中配置代码片段,可以按照以下步骤进行操作: 1. 首先,点击左下角的设置图标,然后选择"用户代码片段"(user snippets)。如果找不到该选项,可以通过右上角的文件菜单选择"首选项",然后再选择"配置用户代码片段"。 2. 在弹出的菜单中,选择你想要配置代码片段的语言,比如JavaScript、Python等。 3. 选择语言后,会打开一个代码片段模板文件。在这个文件中,可以定义你自己的代码片段。 4. 代码片段文件的格式是JSON。每个代码片段一个唯一的名称、一个前缀(用于触发代码片段的快捷键)和一段代码组成。 5. 在定义代码片段时,可以使用一些特殊的变量,比如$1、$2等。这些变量表示代码片段中的占位符,方便你在使用代码片段时进行快速替换。 6. 定义完代码片段后,保存文件。 7. 现在,你可以在编代码的时候使用你刚刚配置的代码片段了。只需要输入代码片段的前缀,然后按下Tab键即可自动插入代码片段。 通过配置代码片段,你可以在开发时快速输入重复使用的代码,提高开发效率。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [VSCode 如何配置用户代码片段](https://blog.csdn.net/qq_57152807/article/details/129022293)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值