我正试图让axios与请求拦截器一起工作.然而
import VueRouter from 'vue-router';
import Login from './components/Login.vue'
import Home from './components/Home.vue'
import axios from 'axios';
window.Vue = require('vue');
window.axios = axios.create({
baseURL: 'http://localhost:8080',
timeout: 10000,
params: {} // do not remove this, its added to add params later in the config
});
Vue.use(VueRouter);
// Check the user's auth status when the app starts
// auth.checkAuth()
const routes = [
{ path: '/', component: Login, name: 'login' },
{ path: '/home', component: Home, name: 'home', beforeEnter: requireAuth },
];
const router = new VueRouter({
routes // short for `routes: routes`
});
const app = new Vue({
router
}).$mount('#app');
function requireAuth (to, from, next) {
if (!loggedIn()) {
router.push('/');
} else {
next()
}
}
function loggedIn() {
return localStorage.token !== undefined;
}
axios.interceptors.request.use(function (config) {
alert('test');
return config;
}, function (error) {
// Do something with request error
return Promise.reject(error)
})
当我在另一个vue文件中使用axios时:
axios.get('users').then((data) => {
console.log(data);
});
拦截器没有被触发!