| 1234567891011121314151617181920212223242526272829303132333435363738394041 |
- export default function ({ $axios, redirect, app, store }, inject) {
- $axios.onRequest(config => {
- if(store.state.token){
- config.headers.common['Authorization'] = 'Bearer ' + store.state.token;
- }
- return config;
- });
- $axios.setHeader('Content-Type', 'application/json');
- $axios.onResponse(response =>{
- if(response.status === 401){
- this.$router.push('/login');
- this.$message({
- showClose: true,
- message: '未授权,请先登录',
- type: 'warning'
- });
- }
- });
- // Create a custom axios instance
- const api = $axios.create({
- // headers: {
- // common: {
- // Accept: 'text/plain, */*'
- // }
- // },
- baseURL: '/api',
- timeout: 10000,
- withCredentials: true
- });
- // Set baseURL to something different
- // api.setBaseURL('https://my_api.com')
- // Inject to context as $api
- inject('api', api)
- }
|