VS code 自定義快捷輸入

小賢筆記發表於2018-07-27

位置

  • ctrl+shift+p
  • 搜尋: snippets
  • 輸入型別: 比如 htmljavascript

引數說明

  • prefix:使用程式碼段的快捷入口
  • body:需要設定的程式碼放在這裡,字串間換行的話使用\r\n換行符隔開.如果值裡包含特殊字元需要進行轉義,多行程式碼以”,”分隔(在引號後面寫逗號)
  • $0:定義最終游標位置
  • $1:定義第一次游標位置,按tab鍵可進行快速切換, 還可以有 $2, $3, $4, $5
  • description:程式碼段描述,在使用智慧感知時的描述

常用字元說明

  • \\ 反斜槓
  • \a 警告
  • \b 退格符
  • \f 換頁符
  • \n 換行符
  • \r 回車符
  • \t Tab 符
  • \v 垂直 Tab 符
  • \u 使用數字指定的Unicode 字元, 如 \u2000
  • \x 使用十六進位制數指定的Unicode 字元, 如 \xc8
  • \0 空值

HTML

    //自定義
    "phone": {
        "prefix": "ph",
        "body": [
            "<!DOCTYPE html>",
            "<html lang='en'>",
            "<head>",
            "    <meta charset='UTF-8'>",
            "    <meta http-equiv='X-UA-Compatible' content='IE=edge,chrome=1'>",
            "    <meta name='viewport' content='width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0'/>",
            "    <title>Titile</title>",
            "</head>",
            "<body>",
            "<script type='text/javascript'>",
            "    $('html').css({fontSize:$(window).width()/375*100 + 'px'});",
            "    $('body').css({minHeight:$(window).height()});",
            "</script>",
            "</body>",
            "</html>"
        ],
        "description": "script"
    },
    "script": {
        "prefix": "sc",
        "body": [
            "<script type='text/javascript' src='$1'></script>"
        ],
        "description": "script"
    }

JavaScript

    // 自定義
    "function": {
        "prefix": "fn",
        "body": [
            "function$1($2) {\r\n\t$3\r\n}"
        ],
        "description": "function(){}"
    },
    "console.log": {
        "prefix": "co",
        "body": [
            "console.log ($1);"
        ],
        "description": "console.log()"
    },
    "alert": {
        "prefix": "al",
        "body": [
            "alert ($1);"
        ],
        "description": "alert()"
    },
    "class": {
        "prefix": "cl",
        "body": [
            "$('.$1')"
        ],
        "description": "class"
    },
    "id": {
        "prefix": "id",
        "body": [
            "$('#$1')"
        ],
        "description": "id"
    },
    "on": {
        "prefix": "on",
        "body": [
            "on ('click',function () {\r\n\t$1\r\n})"
        ],
        "description": "on"
    },
    "for": {
        "prefix": "fo",
        "body": [
            "for (let i=0; i<$1; i++) {\r\n\t$2\r\n}"
        ],
        "description": "for"
    },
    "if": {
        "prefix": "ifif",
        "body": [
            "if ($1) {\r\n\t$2\r\n}"
        ],
        "description": "if"
    },
    "ifElse": {
        "prefix": "ifel",
        "body": [
            "if ($1) {\r\n\t$2\r\n} else {\r\n\t$3\r\n}"
        ],
        "description": "ifElse"
    },
    "ajax": {
        "prefix": "aj",
        "body": [
            // "$.ajax({\r\n\turl:$1,\r\n\tdata:{\r\n\t\t$2\r\n\t},\r\n\ttype:'post',\r\n\tdataType:'JSON',\r\n\tsuccess:function (res) {\r\n\t\t$3\r\n\t}\r\n})"
            "$.ajax({",
            "    url: $1,",
            "    data:{",
            "        $2",
            "    },",
            "    type: 'post',",
            "    dataType: 'JSON',",
            "    success:function (res) {",
            "        $3",
            "    }",
            "})"
        ],
        "description": "ajax"
    },
    "axios": {
        "prefix": "ax",
        "body": [
            "axios.$1(__PROJECTPATH__ + `$2`, {",
            "   $3",
            "})",
            ".then(function (response) {",
            "   console.log(response);",
            "})",
            ".catch(function (error) {",
            "   console.log(error);",
            "});",
        ],
        "description": "axios"
    }

Vue

注: 元件/例項的書寫順序參照 Vue 風格指南

    // 自定義(移動端)
    "vue": {
        "prefix": "vu",
        "body": [
            "<!-- $1 -->",
            "<template>",
            "    <div>",
            "        $2",
            "    </div>",
            "</template>",
            "",
            "<script>",
            "    export default {",
            "        name: '',",
            "        components: {",
            "            ",
            "        },",
            "        props: {",
            "           ",
            "        },",
            "        data() {",
            "            return {",
            "                ",
            "            }",
            "        },",
            "        computed: {",
            "            ",
            "        },",
            "        watch: {",
            "           ",
            "        },",
            "        created() {",
            "            ",
            "        },",
            "        methods: {",
            "            ",
            "        },",
            "    }",
            "</script>",
            "",
            "<style scoped lang='less'>",
            "    ",
            "</style>"
        ],
        "description": "Vue templet"
    }

相關文章