前文我們已經封裝了axios的get請求和post請求,接下來實現對api接口的統一管理
首先在src/request/下新建api.js文件,然后引入封裝的http.js(get,post)
/** * api接口統一管理 */ import { get, post } from './http'
import Vue from 'vue'
然后定義接口
/** * testGet 接口 * @param {Object} p 請求參數 */ export const testGet = p => get(Vue.prototype.api + 'TestGet', p) /** * testPost 接口 * @param {Object} p 請求參數 */ export const testPost = p => post(Vue.prototype.api + 'TestPost ', p)
接口定義完成后可在組件中引入api.js並調用接口
//接口調用 import { testGet, testPost, } from './../../../../request/api';