当前位置:   article > 正文

vue项目_const { data: res } = await this.$http.post(

const { data: res } = await this.$http.post(

前端和服务器之间存在跨域问题就使用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()
})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

aioxs的使用

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 }
      )
  • 1
  • 2
  • 3
  • 4

请求拦截器 相当于一个预处理的过程

在这里插入图片描述

axios.interceptors.request.use(config => {
  // 为请求头对象,添加token验证authorization字段
  config.headers.Authorization=window.sessionStorage.getItem('token')
  return config
})
  • 1
  • 2
  • 3
  • 4
  • 5
本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号