vue:设置全局函数以及调⽤函数1、定义⼀个全局函数⽂件common.js,并定义全局函数
const token = '123456'
// 根据code得到name
const getNameByCode = (code, list, codeProperty, nameProperty) => {
let name = '';
if (code && Array.isArray(list)) {
list.some((record) => {
if (record[codeProperty] === code) {
name = record[nameProperty];
return true;
}
return false;
});
}
return name;
};
export default{
token,
getNameByCode
}
2、绑定到vue上⾯:
在main.js⽂件上⾯,⾸先引⼊全局函数,并绑定的到vue上⾯。
import CommonFunction from './utils/commonFunction';  //引⼊⽂件
Vue.prototype.CommonFunction = CommonFunction;  // 绑定到vue上⾯
3、使⽤全局函数:
直接调⽤参数
token: ken
调⽤函数⽅法:利⽤code得到name
methods:{
  getName: function(parentId) {
  return NameByCode(
parentId,
函数prototype
"id",
"name"
);
 }
}
在模板中调⽤函数⽅法
<template slot-scope="scope">
<span v-if="lumn.property === 'parentId'">
      {{ w.parentId == 0 ? '顶级' : lumn.property])}}</span>
<span v-else>{{lumn.property]}}</span>
</template>