最近在敲代码的过程中遇到了一件很麻烦的事情,在代码中封装了一下fetch请求,由于框架原因呢使用.then().catch()的时候并不多。
但是在最近一期的开发中,有很多次使用.then().catch(),每次都需要在.then()中写入 return res.response&&res.response.result; 敲得次数多了就感觉很是麻烦。
记得vscode有设置代码片段的功能,在网上查找了一下文档解决了烦恼。
创建代码片段方式
mac command+shift+p(windows cont+shift+p 应该是 emm )
打开命令版输入 configure user snippets
以.then为例
{
"input .then": { // 名称
"scope": "javascript,typescript",
"prefix": ".then", //前缀 (使用时输入.then就可以触发)
"body": [ // 内容
".then(res => {",
"return res.response && res.response.result;",
"})",
".then(data => {",
"});"
],
"description": ".then"
}
}
解决问题,很对可以重复使用的代码前端都可以添加,使用会很方便。
网友评论