main.js 中:
import axios from '................/axios'
axios.js 中:
//axios.js import Vue from 'vue' import axios from 'axios' Vue.prototype.$http = axios //http request 封裝請求頭攔截器 axios.interceptors.request.use(config => { // console.log("request") // console.log(config) //請求方式 let method = config.method.toLowerCase(); if (method === 'get' || method === 'delete') { Object.assign(config.params, { "test": "testVAl" }); } return config; }, error => { return Promise.reject(err); }); //http response 封裝后台返回攔截器 axios.interceptors.response.use(response => { // console.log(response) //當返回信息為未登錄或者登錄失效的時候重定向為登錄頁面 // if (response.data.code == 'W_100004' || response.data.message == '用戶未登錄或登錄超時,請登錄!') { // router.push({ // path: "/", // querry: { // redirect: router.currentRoute.fullPath // } //從哪個頁面跳轉 // }) // } if (typeof response.data === 'string') { return JSON.parse(response.data); } else { return response; } }, error => { return Promise.reject(error) });
使用:
this.$http.get('/api/......', {params:{}}).then(res => { console.log(res) }, res => { // error callback });