axios二次封裝

2022-06-30 18:54:16 字數 1891 閱讀 7076

axios封裝

axios請求

axios二次封裝 封裝公共請求

123

4567

891011

1213

1415

1617

1819

2021

2223

2425

2627

2829

3031

3233

import axios from 'axios';

import from 'element-ui';

import from './path'; //所有的請求介面

import from '../util/auth';

import store from "../store";

/*** @param url 請求url 封裝公共請求

* @param method get,post,put,delete

* @param params 引數

*/const getconfig = (url, method, params,type = "json") =>

};// urlsearchparams處理會將引數 json格式改成字串格式 很具需求來 需要就注釋 data: params,

// 設定引數

if (method === 'get') else if (method === 'post')

// 讓每個請求攜帶token--['authorization']為自定義key 請根據實際情況自行修改

if (store.getters.token)

return config_;

};

封裝get方法

123

4567

8910

export function get(group, method, params = {}) ).catch(err => );

});}

封裝post請求

123

4567

891011

1213

14

export function post(group, method, data = {}) 

}, err => );

});}

123

4567

891011

1213

1415

1617

1819

2021

2223

2425

2627

2829

3031

32

封裝上傳

123

4567

891011

1213

1415

1617

1819

2021

2223

2425

123

4567

8

##### 使用方式 main.js引入

```bash

import from './utils/axios';

vue.prototype.$post = post;

vue.prototype.$get = get;

vue.prototype.$downloadfile = downloadfile;

vue.prototype.$uploadfilerequest = uploadfilerequest;

axios二次封裝

引入路由 import router from router index 引入vuex import store from store index 引入提示元件 import from element ui 引入api import apilist from apilist 引入qs let htt...

axios的二次封裝

這次是對axios進行簡單的二次封裝 vue專案中 提供ajax請求 對axios進行二次封裝 判斷是否成功 return this issuccess response 判斷響應結果是否成功 static issuccess res else 構建失敗物件 static requestexpect...

axios的二次封裝

專案中經常用到axios的二次封裝,一次封裝是指通過xhr的封裝,這個不用自己封裝,指axios的底層原始碼,二次封裝指自定義的封裝 這裡的instance是函式物件,儘管它是create出來的,該物件是可以直接作為函式來使用的,它與axios的相似之處是都可以用來傳送請求,引數都為js配置物件,但...