10. VS Code 添加自定義代碼塊詳細(xì)教程 及 部分代碼塊

1. 先 選擇 首選項(xiàng) 里面的 代碼片段 (建議直接選擇 全局作用域)

image

<br />

2. Vue 部分代碼塊

{
    // 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"
    // }
    // 代碼塊里面的 命令 解析
    // scope:作用文件類型。 例如: 指定作用范圍類型"css, javascript"   建議 不用添加這個屬性
    // prefix: 觸發(fā)代碼塊的字符串。  喚醒代碼塊的 快捷命令
    // body:代碼塊的主體內(nèi)容伙判。
    // description:代碼塊的簡單介紹與描述钞翔。
    // 重要:
    // 1  代碼塊書寫格式:  在 原文件 中 將代碼粘貼過來、 每一行的代碼都需要加上 雙引號 ""
    // 2. $ 在代碼塊里不顯示的問題烟具、  $$ 打兩個 $ 即可梢什。 第二個 $ 會被展示出來
    // 3. 多個代碼塊用逗號 ,  隔開
    // 4. Expected comma;缺少逗號的意思朝聋。 檢查代碼塊前后是否缺少逗號嗡午。
    // 5. 選中一樣的英語詞, 再按快捷鍵 Ctrl + D 就可以將多個一樣的英文詞快速選中
    "message error": {
        "prefix": "error", //喚醒代碼塊的命令
        "body": [
            "this.$$message.error('獲取參數(shù)列表失敿胶邸荔睹!')",
        ], //代碼塊的主體
        "description": "error" // 獲取參數(shù)錯誤提示
    },
    "message success": {
        "prefix": "success", //喚醒代碼塊的命令
        "body": [
            "this.$$message.success('獲取參數(shù)列表成功!')",
        ], //代碼塊的主體
        "description": "success" // 獲取參數(shù)成功提示
    },
    "message info": {
        "prefix": "info", //喚醒代碼塊的命令
        "body": [
            "this.$$message.info('獲取參數(shù)提示信息金度!')",
        ], //代碼塊的主體
        "description": "info" // 獲取參數(shù)成功提示
    },
    "message warning": {
        "prefix": "warning", //喚醒代碼塊的命令
        "body": [
            "this.$$message.warning('獲取參數(shù)提示信息应媚!')",
        ], //代碼塊的主體
        "description": "warning" // 獲取參數(shù)警告提示
    },
    // vue 單文件 代碼塊
    "message vueComponent": {
        "prefix": "vueComponent", //喚醒代碼塊的命令
        "body": [
            "<template>",
            "<div>",
            "這是 Home 根組件",
            "</div>",
            "</template>",
            "<script>",
            "export default {",
            "data() {",
            "return {};",
            "},",
            "created() {},",
            "methods: {},",
            // 計算屬性
            "computed: {},",
            "}",
            "</script>",
            // scoped 防止 css 重疊
            // 注意: 這里是單引號  (但應(yīng)該是雙引號)
            "<style lang='less' scoped>",
            "</style>",
        ], //代碼塊的主體
        "description": "vueComponent" // 獲取參數(shù)成功提示
    },
    // 搭建 axios 請求路徑
    "message axiosPath": {
        "prefix": "axiosPath", //喚醒代碼塊的命令
        "body": [
            // 導(dǎo)入 axios 并設(shè)置 默認(rèn)地址
            "import axios from 'axios'",
            "axios.defaults.baseURL = 'http://127.0.0.1:8888/api/private/v1/'",
            "Vue.prototype.$http = axios",
            // 調(diào)用接口
            //   獲取左側(cè)菜單數(shù)據(jù)
            "async getMenuList() {",
            "const {data: res} = await this.$$http.get('menus')",
            "console.log(res)",
            "}",
        ], //代碼塊的主體
        "description": "axiosPath" // 配置 axios 的請求路徑
    },
    // 發(fā)送 http 請求代碼塊 get 請求
    "message get": {
        "prefix": "get", //喚醒代碼塊的命令
        "body": [
            // 在發(fā)送請求之前, 需要添加 awiat  和 async  async 是添加在父級函數(shù)上
            "const {data: res} = await this.$$http.get('')",
        ], //代碼塊的主體
        "description": "get" // 獲取參數(shù)錯誤提示
    },
    // post 請求
    "message post": {
        "prefix": "post", //喚醒代碼塊的命令
        "body": [
            // 在發(fā)送請求之前猜极, 需要添加 awiat  和 async  async 是添加在父級函數(shù)上
            "const {data: res} = await this.$$http.post('')",
        ], //代碼塊的主體
        "description": "post" // 獲取參數(shù)錯誤提示
    },
    // put 請求
    "message put": {
        "prefix": "put", //喚醒代碼塊的命令
        "body": [
            // 在發(fā)送請求之前中姜, 需要添加 awiat  和 async  async 是添加在父級函數(shù)上
            "const {data: res} = await this.$$http.put('')",
        ], //代碼塊的主體
        "description": "put" // 獲取參數(shù)錯誤提示
    },
    // delete 請求
    "message delete": {
        "prefix": "delete", //喚醒代碼塊的命令
        "body": [
            // 在發(fā)送請求之前, 需要添加 awiat  和 async  async 是添加在父級函數(shù)上
            "const {data: res} = await this.$$http.delete('')",
        ], //代碼塊的主體
        "description": "delete" // 獲取參數(shù)錯誤提示
    },
    //  添加 全局樣式 global
    "message global": {
        "prefix": "global", //喚醒代碼塊的命令
        "body": [
            "html, body, #app {",
            "height: 100%;",
            "margin: 0;",
            "padding: 0;",
            "}",
            // 導(dǎo)入 css 全局樣式  在main.js中導(dǎo)入 css文件
            // 不要忘記導(dǎo)入到某一個 css 的頁面
            "import './assets/css/global.css'",
        ], //代碼塊的主體
        "description": "global" // 獲取參數(shù)錯誤提示
    },
    // vue table 展開列 和 索引列
    "message expand": {
        "prefix": "expand", //喚醒代碼塊的命令
        "body": [
            "<!-- 展開列  下面的單引號建議改為雙引號 -->",
            "<el-table-column type='expand'></el-table-column>",
            "<!-- 添加 index 索引 -->",
            "<el-table-column type='index' label='#'></el-table-column>",
        ], //代碼塊的主體
        "description": "expand" // 獲取參數(shù)錯誤提示
    },
    // vue form 表單重置
    "message reset": {
        "prefix": "reset", //喚醒代碼塊的命令
        "body": [
            // 重置表單  addFormRef 是表單的引用
            "this.$$refs.addFormRef.resetFields()",
        ], //代碼塊的主體
        "description": "reset" // 獲取參數(shù)錯誤提示
    },
    // vue form 表單預(yù)驗(yàn)證
    "message valid": {
        "prefix": "valid", //喚醒代碼塊的命令
        "body": [
            // 表單預(yù)驗(yàn)證  addFormRef 是表單的引用
            "this.$refs.addFormRef.validate(valid => {",
            // console.log(validate) 返回 的是 false 和 true
            // true 表示 預(yù)驗(yàn)證 通過跟伏、 false 表示 預(yù)驗(yàn)證 沒通過
                "if(!valid) {",
                  "return this.$$message.error('請?zhí)砑颖匾膮?shù)項(xiàng)6摺!')",
                "}",
                // 執(zhí)行添加的業(yè)務(wù)邏輯  表單校驗(yàn)完成后受扳, 就可以發(fā)起請求了
                "this.$$message.success('成功携龟!')",
        
              "})",
        ], //代碼塊的主體
        "description": "valid" // 獲取參數(shù)錯誤提示
    },
    // vue Message Box 彈框
    "message messageBox": {
        "prefix": "messageBox", //喚醒代碼塊的命令
        "body": [
            // 根據(jù)Id刪除對應(yīng)的用戶信息
            "async removeUserById() {",
            "const confirmResult = await this.$$confirm('此操作將永久刪除該用戶, 是否繼續(xù)?', '提示', {",
            "confirmButtonText: '確定',",
            "cancelButtonText: '取消',",
            "type: 'warning'",
            "}).catch( err => {",
            "return err",
            "})",
            // 如果用戶確認(rèn)刪除,則返回值為字符串 confirm
            // 如果用戶取消了刪除勘高,則返回值為字符串 cancel
            // console.log(confirmResult)
            "if(confirmResult !== 'confirm') {",
            "return this.$$message.info('已取消刪除該用戶')",
            "}",
            "console.log('刪除成功')",
        ], //代碼塊的主體
        "description": "messageBox" // Message Box 彈框
    },
    // console.log() 代碼塊
    "message log": {
        "prefix": "log", //喚醒代碼塊的命令
        "body": [
            "console.log('ok')",
        ], //代碼塊的主體
        "description": "log" // log 的代碼塊
    },
    // 作用域插槽 代碼塊
    "message scope": {
        "prefix": "scope", //喚醒代碼塊的命令
        "body": [
            "<template slot-scope='scope'>",
            "</template>"
        ], //代碼塊的主體
        "description": "scope" // scope 的代碼塊
    },
    // 定義全局的時間過濾器 dataFormat 調(diào)用
    "message dataFormat": {
        "prefix": "dataFormat", //喚醒代碼塊的命令
        "body": [
            // 定義一個全局的 時間過濾器
            "Vue.filter('dataFormat', function(originVal) {",

                "const dt = new Date(originVal)",
            
                // 年月日 y-m-d
                "const y = dt.getFullYear()",
                // 月份是從 0 開始的峡蟋,所以需要加1 
                // padStart() 方法用另一個字符串填充當(dāng)前字符串
                "const m = (dt.getMonth() + 1 +'').padStart(2, '0')",
                "const d = (dt.getDate() + '').padStart(2, '0')",
            
                // 小時分鐘秒  hh:mm:ss
                "const hh = (dt.getHours() + '').padStart(2, '0')",
                "const mm = (dt.getMinutes() + '').padStart(2, '0')",
                "const ss = (dt.getSeconds() + '').padStart(2, '0')",
            
                // return 年月日 時分秒
                "return `${y",
                            "}-${m",
                            "}-${d",
                            "} ${hh",
                            "}:${mm",
                            "}:${ss",
                            "}`",
            "})",
        ], //代碼塊的主體
        "description": "dataFormat" // 時間過濾器 將時間戳轉(zhuǎn)換為年月日 時分秒
    },
    // 表單校驗(yàn)規(guī)則 required
    "message required": {
        "prefix": "required", //喚醒代碼塊的命令
        "body": [
            "{ required: true, message: '請輸入商品名稱', trigger: 'blur' },",
        ], //代碼塊的主體
        "description": "required" // 表單校驗(yàn)規(guī)則
    },
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末坟桅,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子蕊蝗,更是在濱河造成了極大的恐慌仅乓,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,270評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蓬戚,死亡現(xiàn)場離奇詭異夸楣,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)子漩,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,489評論 3 395
  • 文/潘曉璐 我一進(jìn)店門豫喧,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人幢泼,你說我怎么就攤上這事紧显。” “怎么了旭绒?”我有些...
    開封第一講書人閱讀 165,630評論 0 356
  • 文/不壞的土叔 我叫張陵鸟妙,是天一觀的道長。 經(jīng)常有香客問我挥吵,道長重父,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,906評論 1 295
  • 正文 為了忘掉前任忽匈,我火速辦了婚禮房午,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘丹允。我一直安慰自己郭厌,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,928評論 6 392
  • 文/花漫 我一把揭開白布雕蔽。 她就那樣靜靜地躺著折柠,像睡著了一般。 火紅的嫁衣襯著肌膚如雪批狐。 梳的紋絲不亂的頭發(fā)上扇售,一...
    開封第一講書人閱讀 51,718評論 1 305
  • 那天,我揣著相機(jī)與錄音嚣艇,去河邊找鬼承冰。 笑死,一個胖子當(dāng)著我的面吹牛食零,可吹牛的內(nèi)容都是我干的困乒。 我是一名探鬼主播,決...
    沈念sama閱讀 40,442評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼贰谣,長吁一口氣:“原來是場噩夢啊……” “哼娜搂!你這毒婦竟也來了迁霎?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,345評論 0 276
  • 序言:老撾萬榮一對情侶失蹤百宇,失蹤者是張志新(化名)和其女友劉穎欧引,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體恳谎,經(jīng)...
    沈念sama閱讀 45,802評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,984評論 3 337
  • 正文 我和宋清朗相戀三年憋肖,在試婚紗的時候發(fā)現(xiàn)自己被綠了因痛。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,117評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡岸更,死狀恐怖鸵膏,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情怎炊,我是刑警寧澤谭企,帶...
    沈念sama閱讀 35,810評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站评肆,受9級特大地震影響债查,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜瓜挽,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,462評論 3 331
  • 文/蒙蒙 一盹廷、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧久橙,春花似錦俄占、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,011評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至祝拯,卻和暖如春甚带,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背鹿驼。 一陣腳步聲響...
    開封第一講書人閱讀 33,139評論 1 272
  • 我被黑心中介騙來泰國打工欲低, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人畜晰。 一個月前我還...
    沈念sama閱讀 48,377評論 3 373
  • 正文 我出身青樓砾莱,卻偏偏與公主長得像,于是被迫代替她去往敵國和親凄鼻。 傳聞我的和親對象是個殘疾皇子腊瑟,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,060評論 2 355

推薦閱讀更多精彩內(nèi)容