美文网首页
基础请求封装

基础请求封装

作者: 风雪夜丶 | 来源:发表于2019-05-31 10:10 被阅读0次

在请求封装页面xx.js中

import axios from 'axios'

// axios 基本配置
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API, // 请求域名 url = base url + request url
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 5000 // 请求超时时间
})

// 添加请求拦截器
service.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么
    return config;
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});

// 添加响应拦截器
service.interceptors.response.use(function (response) {
    // 对响应数据做点什么
    return response;
}, function (error) {
    // 对响应错误做点什么
    return Promise.reject(error);
});

export function axiosPost(url,data={}) {
    return new Promise((resolve, reject) => {
        service.post(url,data).then(res=>{
            resolve(res.data)
        }),err=>{
            reject(err)
        }
    })
}

在统一的api文件中

import { axiosPost } from "../http/request";

export const indexInfo = (data={})=>axiosPost('url',data);

使用页面中

async getInfo(){
    const Info = await indexInfo()
    this.AboutInfo = Info
    console.log(Info)
 }

相关文章

  • Koa(五、源码浅析)

    基础http请求 针对http基础请求简单封装 效果上面两者相同,下面继续封装 js的getter和setter ...

  • 基础请求封装

    在请求封装页面xx.js中 在统一的api文件中 使用页面中

  • 12.第三方库

    简介 网络请求-Alamofire Alamofire 是在苹果 URL Loading System 基础上封装...

  • Vue 网络请求封装

    1.本片是最基础的网络请求封装: 将header,以及 onSuccess,和 onFail 封装。 只需要将ur...

  • RXSwift基础网络请求封装

    一、创建项目,集成cocoapods文件 这里使用的是Moya、SwiftyJSON、Alamofire进行网络请...

  • 基础模块封装 -- 网络请求

    一、网络请求抽象类 二、网络请求实现类 三、网络请求实体类 四、网络请求实体工具类 五、网络响应头信息类 六、网络...

  • uni-app 服务端请求封装

    自己封装的uni-app服务端请求,包括 异常捕获、loading开启关闭、特殊请求基础地址、header的tok...

  • uniapp请求+uView2.0请求封装

    uniapp开发基础模板,对uniapp请求进行了简单封装,实现请求拦截及响应处理,同时引用了 uView2.0 ...

  • Fetch

    api/base.js 基础域名 api/index.js api集中管理 utils/http.js 封装请求 ...

  • 组件化方案实践总结

    1.模块分离 1.基础模块基础模块主要封装网络请求,日志框架,路由设置等信息 业务模块业务模块依赖基础模块,实现不...

网友评论

      本文标题:基础请求封装

      本文链接:https://www.haomeiwen.com/subject/iuzitctx.html