亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

vue 這樣的函數,怎么提取為公共函數?

vue 這樣的函數,怎么提取為公共函數?

嚕嚕噠 2018-10-24 08:50:57
在一個組件里有這樣一個方法:name: 'user', data() {  const checkExist = (rule, value, callback) => {    if (value) {      this.$http.get(....).then(res => {        if (res.id === this.itemId) {           ...         }       });     }     callback();   };  return {    itemId: '',    rules: {      name: [{ validator: checkExist, message: ‘名稱已存在’ }];     },   }; },其中這個checkExist如何提取為公共函數,可以在別的組件中直接應用。特別有一點不清楚就是函數里使用了axios的封裝this.$http,提取為公共函數后該如何處理比較科學?還有如何處理this.itemId的引用?
查看完整描述

1 回答

?
飲歌長嘯

TA貢獻1951條經驗 獲得超3個贊

推薦我的這種寫法:

// utils.jsexport const checkExist = http => (rule, value, cb) => {    if (value) {
        http() // 
    }
    cb()
}// 調用import { setUpcheckExit } from 'utils'const checkExist = setUpcheckExit(this.$http)
...

data() {    return {        rules: {            name: [{ validator: checkExist, message: ‘名稱已存在’ }];
        }
    }
}


查看完整回答
反對 回復 2018-10-24
  • 1 回答
  • 0 關注
  • 1324 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號