如何保存token的两种方法

1.在vue中保存

首先安装包:

npm i js-cookie //安装

在入口文件main.js中引用:

import cookie from 'js-cookie';
Vue.prototype.$cookie = cookie; 

然后赋值调用:

var $cookie = this.$cookie;
//赋值
this.$cookie.set('token',$result.data.token)
//调用
headers:{
       Authorization:this.$cookie.get('token')
      }
//删除token
this.$cookie.remove("token")      

2.在localStorage保存

localstorage方法赋值

localStorage.setItem("key_token", result.data.token);

删除 localstorage指定键对应的值

localStorage.removeItem('token');

删除 localstorage所有值

localStorage.clear('token');

你可能感兴趣的:(token,前端,ajax)