Home > Web Front-end > JS Tutorial > How to set the login permission of routing in vue

How to set the login permission of routing in vue

不言
Release: 2018-07-03 14:05:16
Original
1832 people have browsed it

This article mainly introduces the method of setting login permission for routing in vue. It is very good and has certain reference value. Friends in need can refer to it

index.js

Set meta attributes for routes that require login permission

meta:{requireAuth:true},
Copy after login

main.js

Write the verification of routing directly in main.js

router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requireAuth)){ // 判断该路由是否需要登录权限
    if (sessionStorage.getItem("access_token")) { // 判断当前的token是否存在
      next();
    }
    else {
      next({
      path: '/manage',
      query: {redirect: to.fullPath} // 将跳转的路由path作为参数,登录成功后跳转到该路由
    })
    }
  }
  else {
    next();
  }
});
Copy after login

The above is the entire content of this article, I hope it will be useful for everyone’s learning For help, please pay attention to the PHP Chinese website for more related content!

Related recommendations:

How to use Vue to customize the numeric keyboard component

About vue-scroller recording the scroll position Code introduction

The above is the detailed content of How to set the login permission of routing in vue. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
vue
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template