需求:有几组数据,是好多页面都需要的,后台给每组数据写到一个接口里了,所以我需要请求多个公共接口,我的想法就定义了一个公共js,然后所需的页面去引用;
第一种方法:
common.js:
var commonObj ={ async GetDepartment(fn){ var a =await axios.post('/api/common/getDepartment'); fn(a) }};export default commonObj
所需的vue页:
import commonObj from '@/common/js/common.js' //先引入文件commonObj.GetDepartment(function(d){ console.log(d)})
第二种:
common.js:
export async function GetDepartment(fn){ var a =await axios.post('/api/common/getDepartment'); return a; };
所需的vue页:
import {GetDepartment} from '@/common/js/common.js' //先引入文件 解构GetDepartment().then(d=>{ console.log(d);});
其他方法:https://www.jianshu.com/p/9aa2f6c379dd