赞
踩
前端和服务器之间存在跨域问题就使用token,不存在跨域问题就使用cookie和session来维持状态
创建分支并且切换分支 git checkout -b login
app.vue是根组件
v-model是双向绑定 :model 是v-bind:model的缩写
token 是登录成功后服务器颁发的身份信息
router.beforeEach((to, from, next) => {
// to 将要访问的路径
// from 代表从哪个路径跳转而来
// next 是一个函数,表示放行
// next() 放行 next('/login') 强制跳转
if(to.path=='/login') return next()
const token=window.sessionStorage.token;
if(!token) return next('/login')
next()
})
import axios from ‘axios’
Vue.prototype.$http = axios
axios.defaults.baseURL = ‘http://127.0.0.1:8888/api/private/v1/’
axios使用
const { data: res } = await this.$http.post(
`roles/${this.roleId}/rights`,
{ rids: idStr }
)
axios.interceptors.request.use(config => {
// 为请求头对象,添加token验证authorization字段
config.headers.Authorization=window.sessionStorage.getItem('token')
return config
})
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。