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 .
Menu