Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

我的学习日志(八) #9

Open
3 tasks done
xxxgitone opened this issue Jun 12, 2017 · 1 comment
Open
3 tasks done

我的学习日志(八) #9

xxxgitone opened this issue Jun 12, 2017 · 1 comment

Comments

@xxxgitone
Copy link
Owner

xxxgitone commented Jun 12, 2017

有两天没有写这个日志了,因为将学习的东西写在博客上了。今天开始抢指导老师,做毕业设计,哈哈 😂

完成任务

  • 三小时英语学习
  • 创建对象和继承的方式
  • 完成vnpastiem登录模块

知识点

关于原型的知识在博客里面写得很清楚了

记录一下在多登录模块的使用用到的两个拦截器,一个是vue的路由拦截器,另一个是axios的,两者配合可以发挥最大威力

用户登录后,不适用拦截器的话,一些需要权限才能访问的路由照样可以通过,使用router.beforeEach方法 具体代码

router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) { // 如果需要权限
    if (store.state.token) { // 存在的话,直接跳转过去
      next()
    } else {
      next({ path: '/' })
      store.commit('SHOW_SIGNIN_DIALOG')
    }
  } else {
    next()
  }
})

需要在有权限的路由上,添加一个自定义字段 meta { requireAuth: true }

我们使用token来进行跟后台交互,每次向后台请求资源的时候都得发送token使用axios拦截器,可以向请求添加一个头部信息,携带token信息。具体代码,创建了一个axios服务

如果给token的key为大写的话,发送给后台的时候会统一转换成小写,获取的时候,需要注意

@xxxgitone
Copy link
Owner Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant