0

0

VSCode新手入门之浅析代码片段,看看创建方法

青灯夜游

青灯夜游

发布时间:2022-01-24 19:15:25

|

3500人浏览过

|

来源于掘金社区

转载

本篇文章带大家了解一下vscode中的代码片段,介绍一下代码块种类,以及自定义代码片段的方法,希望对大家有所帮助!

VSCode新手入门之浅析代码片段,看看创建方法

一、前言

较为全的指南:

《VS Code 代码片段完全入门指南》https://chinese.freecodecamp.org/news/definitive-guide-to-snippets-visual-studio-code/

一键生成代码块工具:https://snippet-generator.app/

Windows系统: 文件 > 首选项 > 用户代码片段 Mac系统: Code > 首选项 > 用户片段

二、创建

代码块种类

  • 全局代码片段(每种语言环境下都能触发代码块):新建全局代码片段会在 snippets 目录下生成 .code-snippets 为后缀的配置文件;【推荐学习:《vscode入门教程》】

  • 针对特定语言类型(只能在对应语言环境下才能触发):而新建对应语言的代码片段会生成 对应语言 + .json 的配置文件;

  • 为某一工作区(项目)创建的代码块;

1.png

2.png

3.png

新建

输入 react 自动创建一个 react.code-snippets 文件,全局创建则在本机文档目录,项目创建则在项目目录内;

4.png

{
  // Place your 全局 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"
  // }
}

创建了一个 dva 的模版:

Nimo.space
Nimo.space

智能画布式AI工作台

下载
{
  // Place your 全局 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"
  // }

  // dva 基础布局结构
  "dva-basic": {
    "prefix": "lll_dva_basic",
    "body": [
      "import { Effect, Reducer, Subscription } from 'umi';",
      "",
      "export interface ${1:xxxxModelType} {",
      "  namespace: '${2:xxxx}';",
      "  state: ${3:IxxxxModelState};",
      "  effects: {",
      "    initDataEffect: Effect;",
      "  };",
      "  reducers: {",
      "    updateState: Reducer<${3:IxxxxModelState}>;",
      "  };",
      "  subscriptions: { setup: Subscription };",
      "}",
      "",
      "export interface ${3:IxxxxModelState} {",
      "  ${4:textData}: any;",
      "}",
      "",
      "const state: ${3:IxxxxModelState} = {",
      "  ${4:textData}: null,",
      "};",
      "",
      "const QualificationSetting: ${1:xxxxModelType} = {",
      "  namespace: '${2:xxxx}',",
      "  state: state,",
      "",
      "  effects: {",
      "    // 初始化数据",
      "    *initDataEffect({ payload }, { select, call, put }) {",
      "      try {",
      "      } catch (error) {}",
      "    },",
      "  },",
      "",
      "  reducers: {",
      "    updateState(state, { data }) {",
      "      return { ...state, ...data };",
      "    },",
      "  },",
      "",
      "  subscriptions: {",
      "    setup({ dispatch, history }) {",
      "      return history.listen(({ pathname }) => {",
      "        if (pathname === '/') {",
      "          // 初始化数据",
      "          dispatch({ type: 'initDataEffect' });",
      "        }",
      "      });",
      "    },",
      "  },",
      "};",
      "",
      "export default QualificationSetting;",
      ""
    ],
    "description": "dva-basic"
  }ƒ
}

字段解释

  • "dva-basic" 是代码片段的名字。如果没有 description,它就会出现在智能建议的列表里。

  • prefix 属性定义了代码片段的触发文本。它可以是一个字符串或者一个字符串数组(如果你想有多个触发文本)。前缀的子字符串同样可以触发,在我们的例子里,输入"h1"一样能匹配到我们的代码片段。

  • body 属性代表了要插入编辑器的内容。它是一个字符串数组,可能一行或者多行。在插入之前会被合并成一段。

  • description 属性提供了代码片段的更多描述。它是可选的。

  • scope 属性允许你指定特定的语言类型,你可以使用逗号来分割多种语言。它也是可选的。当然,对于特定于语言的代码片段文件来说是多余的。

Tab Stops (使用 tabs 切换,还有很多用法自行挖掘,比如可选项)

Tab stops由  ** 和 序号 组成,就像 **  和 **序号** 组成,就像 `11代表了第一个位置,1`代表了第一个位置,`2代表了第二个位置,以此类推。$0`代表退出代码片段,以及最后光标停留的位置;

多个 tab 停留,使用相同序号即可;

三、后记

  • 本文章是快速入门指南;

  • 深入学习可参考前言中的较为完整的指南;

  • 可使用前言中的快速生成工具,然后再完善;

更多关于VSCode的相关知识,请访问:vscode教程!!

相关专题

更多
json数据格式
json数据格式

JSON是一种轻量级的数据交换格式。本专题为大家带来json数据格式相关文章,帮助大家解决问题。

412

2023.08.07

json是什么
json是什么

JSON是一种轻量级的数据交换格式,具有简洁、易读、跨平台和语言的特点,JSON数据是通过键值对的方式进行组织,其中键是字符串,值可以是字符串、数值、布尔值、数组、对象或者null,在Web开发、数据交换和配置文件等方面得到广泛应用。本专题为大家提供json相关的文章、下载、课程内容,供大家免费下载体验。

533

2023.08.23

jquery怎么操作json
jquery怎么操作json

操作的方法有:1、“$.parseJSON(jsonString)”2、“$.getJSON(url, data, success)”;3、“$.each(obj, callback)”;4、“$.ajax()”。更多jquery怎么操作json的详细内容,可以访问本专题下面的文章。

310

2023.10.13

go语言处理json数据方法
go语言处理json数据方法

本专题整合了go语言中处理json数据方法,阅读专题下面的文章了解更多详细内容。

75

2025.09.10

js 字符串转数组
js 字符串转数组

js字符串转数组的方法:1、使用“split()”方法;2、使用“Array.from()”方法;3、使用for循环遍历;4、使用“Array.split()”方法。本专题为大家提供js字符串转数组的相关的文章、下载、课程内容,供大家免费下载体验。

258

2023.08.03

js截取字符串的方法
js截取字符串的方法

js截取字符串的方法有substring()方法、substr()方法、slice()方法、split()方法和slice()方法。本专题为大家提供字符串相关的文章、下载、课程内容,供大家免费下载体验。

209

2023.09.04

java基础知识汇总
java基础知识汇总

java基础知识有Java的历史和特点、Java的开发环境、Java的基本数据类型、变量和常量、运算符和表达式、控制语句、数组和字符串等等知识点。想要知道更多关于java基础知识的朋友,请阅读本专题下面的的有关文章,欢迎大家来php中文网学习。

1468

2023.10.24

字符串介绍
字符串介绍

字符串是一种数据类型,它可以是任何文本,包括字母、数字、符号等。字符串可以由不同的字符组成,例如空格、标点符号、数字等。在编程中,字符串通常用引号括起来,如单引号、双引号或反引号。想了解更多字符串的相关内容,可以阅读本专题下面的文章。

620

2023.11.24

PS使用蒙版相关教程
PS使用蒙版相关教程

本专题整合了ps使用蒙版相关教程,阅读专题下面的文章了解更多详细内容。

23

2026.01.19

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
go语言零基础开发内容管理系统
go语言零基础开发内容管理系统

共34课时 | 2.6万人学习

第二十三期_前端开发
第二十三期_前端开发

共98课时 | 7.5万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号