Home  >  Article  >  Web Front-end  >  Introduction to methods of Vue routing interception and page jump settings

Introduction to methods of Vue routing interception and page jump settings

不言
不言Original
2018-07-04 09:40:084161browse

This article mainly introduces the setting method of vue route interception and page jump. It is very good and has certain reference value. Friends in need can refer to it

Routing settings: router/index.js

export default new Router({ 
 routes: [ 
  { 
   path: '/selfcenter', 
   name: 'selfcenter', 
   meta: { 
    requireAuth: true // 配置此条,进入页面前判断是否需要登陆 
   }, 
   component: selfcenter 
  } 
 ] 
})

main.js:

/* eslint-disable no-new */ 
router.beforeEach((to, from, next) => { 
 if (to.matched.some(res => res.meta.requireAuth)) { // 验证是否需要登陆 
  if (sessionStorage.getItem('sid')) { // 查询本地存储信息是否已经登陆 
   next(); 
  } else { 
   next({ 
    path: '/login', // 未登录则跳转至login页面 
    query: {redirect: to.fullPath} // 登陆成功后回到当前页面,这里传值给login页面,to.fullPath为当前点击的页面 
    }); 
  } 
 } else { 
  next(); 
 } 
});

login.vue:

After successful login:

sessionStorage.setItem('sid', res.data.data.sid); // 设置本地存储信息 
this.$router.push(this.$route.query.redirect); // 跳转至前一页,this.$route.query.redirect是获取上面传递过来的值

The above is the entire content of this article, I hope it will be helpful to everyone Learning is helpful. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

About the method of changing the second-level menu to the third-level menu in the Vue iview-admin framework

Analysis Two methods of simulating data in vue-cli

The above is the detailed content of Introduction to methods of Vue routing interception and page jump settings. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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