路由全局跳转和局部跳转

本文介绍了如何在Vue项目中使用vue-router实现点击登录注册页面跳转到首页,包括局部路由的编程式跳转和通过`router-link`的全局跳转,以及在Home组件中处理School和About页面的局部跳转。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目的

是为了实现点击登录注册页面可以跳到首页,或者是说点击想要跳转一个完全新的页面或者点击导航栏,局部路由跳转,也是之前做项目会遇到的问题,由此拿来记录一下,本文章是测试案例,后期想实现点击登录注册页面可以跳到首页,可以做个路由守卫去进行判断

目前页面分配

Home是主页面,在Home页面下局部跳转到About和School页面,在Home页面下全局跳转到Renewpage页面,分别采用编程式跳转和普通router-link跳转

main.js
//  引入vueRouter
import VueRouter from "vue-router"; 
​
// 引入Vue
import Vue from "vue";
​
// 引入App
import App from './App.vue'
​
// 引入store
import store from '../store/demoindex'
// 引入router
import router from './router'
​
​
​
// 关闭Vue的生产提示
Vue.config.productionTip = false
Vue.use(VueRouter)
​
​
// 创建vm
new Vue({
    el:'#app',
    render : h=>h(App),
    store,
    router,
    beforeCreate() {
        Vue.prototype.$bus = this //安装全局事件总线,$bus就是当前应用的vm
    },
})

App.vue

也就在这个页面放了 router-view

<template>
    <div class="App">
        <router-view></router-view>
    </div>
  </template>
<script>
//下边的js就没有写东西了,

router.js
//引入VueRouter
import VueRouter from 'vue-router'
​
import Home from '@/components/Home.vue'
import Renewpage from '@/components/Renewpage.vue'
import School from '@/components/School.vue'
import About from '@/components/About.vue'
​
​
​
const router = new VueRouter({
    mode:'history',
    // base:'/testvue/',
    routes:[
        // 默认最初路径是Home组件
        {
            path:'/',
            component:Home,
        },
        {
            path:'/home',
            component:Home,//如果这里用别的组件,那么路由跳转就会跳到一个新的页面,因为我们默认的路径是Home组件,在Home组件里多放了个 router-view,则School和About组件页面的渲染就会在Home组件渲染了
            children:[ 
            {  
                path:'school', //二级路由此处一定不要写:/news
                name:'school',
                component:School
            },
            { 
                path:'about',
                name:'about',
                component:About
            },
        ]},
        //全局跳转的路由
        {
            path:'/renewpage',
            name:'renewpage',
            component:Renewpage,
        },
        
    ]
})
​
//暴露router
export default router

Home.vue

为了确保School和About能够局部跳转,在此页面也是放了个router-view

Renewpage则是全局跳转

<template>
    <div class="Home">
      <h1>这个页面是Home页面</h1>
      <!-- <br/> -->
      <h2>this.$router.push路由跳转</h2>
      <button @click="toSchool">School</button>
      <button @click="toAbout">About</button>
      <button @click="toNewPage">跳到新页面</button>
      <br/><br/><br/>
      <h2>router-link路由跳转</h2>
      <router-link active-class="active" to='/home/school'><button>School</button></router-link>
      <router-link active-class="active" to='/home/about'><button>About</button></router-link>
      <router-link class="list-group-item" active-class="active" to="/renewpage"><button>跳到新页面</button></router-link>
      <br/><br/><br/>
      <h2>此下为页面跳转之后的展示</h2>
      
      <router-view ></router-view>
​
    </div>
  </template>
  <script>
  export default {
      name:'Home',
      data() {
          return {
            
          }
      },
      computed:{
      },
      methods:{
        toSchool(){
            this.$router.push({
              name:'school',
              params:{
                title:'我是this.$router.push传过来的School字段params'
              }
            })
        },
        toAbout(){
          this.$router.push({
              name:'about',
              params:{
                title:'我是this.$router.push传过来的About字段params'
              }
            })
        },
        toNewPage(){
          this.$router.push({
              name:'renewpage',
              params:{
                title:'我是this.$router.push传过来的renewpage字段params'
              }
            })
        }
          
      },  
      created(){
​
      }
  }
  
  </script>
  
  <style lang="css">
​
  </style>

About/School/Renewpage.vue

这个页面放自己想放的东西就好

<template>
  <div class="about">
    这个页面是About页面
  </div>
</template>
<script>
​
export default {
    name:'About',
    data() {
        return {
          
        }
    },
}
​
</script>
​

路由全局前置守卫是在每次路由导航跳转时自动触发的回调函数。在Vue中,可以通过创建一个路由实例并调用其beforeEach()方法来声明全局前置守卫。在守卫函数中,可以通过参数tofrom来获取当前导航的目标路由来源路由。在守卫函数中,可以使用next()方法来控制路由跳转。 根据引用\[1\]引用\[2\]的代码,可以看出全局前置守卫的逻辑如下: 1. 如果存在token(登录状态),则执行步骤1。 2. 如果目标路由是登录页或注册页,则阻止路由跳转。 3. 如果存在用户信息name,则直接跳转到目标路由。 4. 如果不存在用户信息name,则尝试通过仓库获取用户信息。 5. 如果获取用户信息出错,则说明token已失效,通知仓库进行用户退出,并跳转到登录页。 6. 如果不存在token,则直接跳转到目标路由。 根据引用\[3\]的描述,当页面刷新时,会进入导航守卫。如果存在登录状态(token),则会进入步骤1。如果进入登录注册之外的其他页面,则会进入步骤2。由于刷新时仓库信息丢失,无法获取到仓库中的用户信息name,因此会进入步骤4。在步骤4中,会通知仓库获取用户信息,如果获取出错,则会进入步骤5,即token失效,通知仓库进行用户退出,并跳转到登录页。 总结起来,路由全局前置守卫的作用是在路由导航跳转前进行一些逻辑判断处理,例如检查登录状态、获取用户信息等,以确保用户在跳转路由时的正确性安全性。 #### 引用[.reference_title] - *1* [Vue-导航守卫——全局前置守卫](https://blog.csdn.net/weixin_63836026/article/details/124758560)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [路由全局前置守卫判断token失效](https://blog.csdn.net/LIMITSSS/article/details/126114803)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值