vue main.js axios配置

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'

import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';


Vue.use(ElementUI);


import axios from 'axios'
import qs from 'qs'
axios.defaults.baseURL='http://localhost:8090/api/'


axios.interceptors.request.use(config=>{
    config.headers.token = window.sessionStorage.getItem('token');
    if(config.method=="post"){
      config.data = qs.stringify(config.data);
      config.headers["Content-Type"] = "application/x-www-form-urlencoded";
      //console.log(config);
    }
    return config;
})


Vue.prototype.$axios = axios


Vue.config.productionTip = false

//路由守衛 路由開始前進行操作
router.beforeEach((to,from,next)=>{
  if(to.path==="/"){
    next();
  }
  const token = window.sessionStorage.getItem('token');

  if(token){
    next();
  }else{
    next("/");
  }
})


/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章