• 技术文章 >开发工具 >VSCode

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

    青灯夜游青灯夜游2022-01-24 19:15:25转载1411

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

    一、前言

    较为全的指南:

    《VS Code 代码片段完全入门指南》

    https://chinese.freecodecamp.org/news/definitive-guide-to-snippets-visual-studio-code/

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

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

    二、创建

    代码块种类

    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 的模版:

    {
      // 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"
      }ƒ
    }

    字段解释

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

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

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

    三、后记

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

    以上就是VSCode新手入门之浅析代码片段,看看创建方法的详细内容,更多请关注php中文网其它相关文章!

    声明:本文转载于:掘金社区,如有侵犯,请联系admin@php.cn删除
    上一篇:总结分享一些前端值得了解的VSCode自动化插件 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • VSCode中怎么配置扩展进行Arduino开发• vscode+babel开发一个智能移除未使用变量的插件(实战)• 手把手从0教你开发一个vscode变量翻译插件• 浅析在vscode中怎么用eslint和prettier• 聊聊VSCode中怎么用snippets,用以提升开发效率!• 浅谈VSCode中怎么调试Electron应用的主进程代码
    1/1

    PHP中文网