SoFunction
Updated on 2025-04-10

vue uses the method of routing guard to determine whether to log in

1. In the router routing file, introduce relevant required files;

import Vue from 'vue'

import Router from 'vue-router'
import {LOGIN} from '../common/js/islogin'
import HelloWorld from '@/components/HelloWorld'
import Login from '@/page/Login'
import Index from '@/page/index/index'(Router);

2. Configure related routes

const router = new Router({

 routes: [
 {
  path: '/',
  redirect: '/login'
 },
 {
  path: '/login',
  component: Login
 },
 {
  path: '/index',
  meta: {
  requireAuth: true, // Add this field to indicate that you need to log in to enter this route  },
  component: Index
 }
 ]
});

3. After the routing is configured, judge the route jump according to the page you need to log in.

((to, from, next) => {
 if () {   //If you need to jump, go down (1) if (true) {   //Discern whether you have logged in. If you have logged in, it means that there is a token, or the token has not expired. You can skip login (2)  if ( === '/login') { //Distinguish whether the next route is the route to be verified (3)  next('/index');   // If it is directly jump to the homepage,  } else {    //If the route does not require verification, then go back directly  next();
  }
 } else {
  ('No');  //If you have not logged in or the token has expired, then jump to the login page  next('/login');
 }
 } else {       //No need to jump, just go down next();
 }
});export default router;

The above method of using router guards to determine whether to log in is all the content I share with you. I hope you can give you a reference and I hope you can support me more.