Vue中axios的封装
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/1502d954dc9de66f3754a43084685aa9.png)
import axios from 'axios'
import { getToken } from '@/utils'
const http = axios.create({
baseURL: 'http://localhost:8080',
})
http.interceptors.request.use(
config => {
config.headers = {
token: getToken()
}
return config;
},
err => Promise.reject(err)
);
http.interceptors.response.use(res=>{
console.log("响应拦截");
return res;
}, err=>{
return Promise.reject(err)
});
export const get = (url, param) => {
return http.get(url, param)
}
export const post = (url, param) => {
return http.post(url, param)
}
export const put = (url, param) => {
return http.put(url, param)
}
export const del = (url, param) => {
return http.del(url, param)
}
import { post,get,put,del } from './request'
export const login = param => post("/admin/login",param)
export const checkLoginState = param => get("/admin/checkLoginState",param)
export const listArticle = () => get("/admin/article")
login({ username: this.username, password: this.password }).then(
(response) => {
let data = response.data;
if (data.code == 200) {
message.success(data.msg)
window.localStorage.setItem("token", data.data.token);
window.localStorage.setItem("username", this.username)
this.$router.push("/admin");
} else {
message.error(data.msg)
}
}