计算机系统应用教程网站

网站首页 > 技术文章 正文

vue 请求公共接口

btikc 2024-09-20 15:01:50 技术文章 20 ℃ 0 评论

需求:有几组数据,是好多页面都需要的,后台给每组数据写到一个接口里了,所以我需要请求多个公共接口,我的想法就定义了一个公共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

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表