安装插件
服务封装
项目src/service/index.js文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| import axios from "axios" import { Message } from "element-ui" import $store from '@/store' const httpCodes = require("@/config/http-code.json")
axios.defaults.headers = { 'Content-Type': 'application/json; charset=utf-8' }
axios.interceptors.request.use( (request) => { const token = localStorage.getItem('token'); request.headers['Authorization'] = "Bearer " + token; return request; }, (error) => { return Promise.reject(error); } );
axios.interceptors.response.use( (response) => { if (response.data.code === 2001 && location.pathname !== "/logon") { $store.commit("logout", "1"); } if (response.data.code !== 0) { Message.error(response.data.msg); } return response.data; }, (error) => { const item = httpCodes.find(item => { return error.response.status === item.code; }) Message.error(`${item.code}:${item.message}`); return Promise.reject(error) } );
export default axios;
|
接口封装
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| import http from './index'
export function myTask (params, config) { return http.get("/service/demo/myTask.json", params, config) }
export function taskEntrust: (params, config) { return http.post("/service/demo/setTaskAssignee", params, config) }
export function myFinished: (params, config) { return http.get("/service/demo/myFinished.json", params, config) }
|
可以使用了