前端VUE 封装JS 访问Token

import axios from "axios"; //引入axios

//初始化
const $http = axios.create({
    baseURL:'http://localhost:39052/api/',
    timeout:8000 //超时时间
})

//请求拦截
$http.interceptors.request.use(config=>{
    const token = localStorage.getItem('token');
    if(token){
        config.headers.Authorization = 'Bearer ' + token;
    }
    return config;
})

//公出
export default $http

 

 

在 main.js 里 我们 封装成公共 属性

import http from './resquest/http'
Vue.prototype.$http = http;

 

posted @ 2021-11-17 13:09  姚睿的博客  阅读(217)  评论(0)    收藏  举报