How can a function like vue be extracted as a common function?

there is a method in a component:

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:  ""  }];
    },
  };
},

how this checkExist is extracted as a public function can be directly applied in other components. One thing that is particularly unclear is how to deal with it scientifically after using axios"s wrapper this.$http, extraction as a common function in the function. And what about references to this.itemId?

Jun.28,2021

recommend my way of writing:

// utils.js


export 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:  ''  }];
        }
    }
}

whether the same http request is invoked in different checks, or whether different checks call different requests.

if it is the same request, put checkExist directly in a similar tool function file.

  vuex 
 
  • and just like above, it is referenced in the form of import & & export .
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e945ce-4e99d.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e945ce-4e99d.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?