【Vue3】路由基础

【Vue3】路由基础

背景

随着年龄的增长,很多曾经烂熟于心的技术原理已被岁月摩擦得愈发模糊起来,技术出身的人总是很难放下一些执念,遂将这些知识整理成文,以纪念曾经努力学习奋斗的日子。本文内容并非完全原创,大多是参考其他文章资料整理所得,感谢每位技术人的开源精神。

简介

本文介绍 Vue3 中路由的基本写法。

开发环境

分类 名称 版本
操作系统 Windows Windows 11
IDE Visual Studio Code 1.91.1

开发步骤及源码

1> 创建 Vue3 工程,参考:【Vue3】工程创建及目录说明。

2> 删除 src 目录下 assets 和 components 目录。

3> 修改 src 目录下 main.ts。

复制代码
import { createApp } from 'vue'
import App from './App.vue'

createApp(App).mount('#app')

4> 创建三个页面组件,注意与功能组件不同,不放在 src/components 目录下,页面组件一般放在 pages 或 views 目录下。

  • Dashboard.vue

    复制代码
    <template>
        <div class="dashboard">
            这是仪表盘页面
        </div>
    </template>
    
    <script setup lang="ts">
    </script>
    
    <style scoped lang="scss">
    </style>
  • System.vue

    复制代码
    <template>
        <div class="system">
            这是系统管理页面
        </div>
    </template>
    
    <script setup lang="ts">
    </script>
    
    <style scoped lang="scss">
    </style>
  • About.vue

    复制代码
    <template>
        <div class="about">
            这是关于页面
        </div>
    </template>
    
    <script setup lang="ts">
    </script>
    
    <style scoped lang="scss">
    </style>

5> 执行 npm i vue-router 命令安装路由组件。

6> 在 src 下创建 router 目录,并在其中创建 index.ts 文件,此文件作用是创建并暴露路由器。

复制代码
import { createRouter, createWebHistory } from 'vue-router'
import Dashboard from '@/pages/Dashboard.vue'
import System from '@/pages/System.vue'
import About from '@/pages/About.vue'

const router = createRouter({
    // 路由器工作模式
    history: createWebHistory(),
    routes: [
        {
            path: '/dashboard',
            component: Dashboard
        },
        {
            path: '/system',
            component: System
        },
        {
            path: '/about',
            component: About
        }
    ]
})

export default router

7> 修改 main.ts 引入并使用路由器。

复制代码
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'

createApp(App)
    .use(router)
    .mount('#app')

8> 修改根组件 App.vue,调用路由器实现跳转功能。

复制代码
<template>
  <h1 class="title">Vue3路由</h1>
  <hr>
  <div class="route">
    <div class="menu">
      <div class="menu-item">
        <RouterLink to="/dashboard" active-class="active">仪表盘</RouterLink>
      </div>
      <div class="menu-item">
        <RouterLink to="/system" active-class="active">系统管理</RouterLink>
      </div>
      <div class="menu-item">
        <RouterLink to="/about" active-class="active">关于</RouterLink>
      </div>
    </div>
    <div class="content">
      <RouterView />
    </div>
  </div>
</template>

<script setup lang="ts">
import { RouterLink, RouterView } from 'vue-router'
</script>

<style scoped lang="scss">
.title {
  text-align: center;
}
.route {
  display: flex;
  justify-content: center;
  .menu {
    width: 200px;
    height: 500px;
    background-color: #F1F2F3;
    border-radius: 6px;
    .menu-item {
      height: 40px;
      line-height: 40px;
      text-align: center;
      margin: 5px;
      border-radius: 3px;
      text-decoration: none;
    }
    .menu-item:hover {
      background-color: white;
      cursor: pointer
    }
    a {
      text-decoration: none;
    }
    .active {
      color: #00AEEC;
    }
  }
  .content {
    width: 600px;
    height: 500px;
    margin-left: 10px;
    border: 1px solid #F1F2F3;
  }
}
</style>

9> 执行命令 npm run dev 启动应用,浏览器访问:http://localhost:5173/,点击左侧菜单观察页面变化。

总结

关键步骤:

  1. 安装路由组件:npm i vue-router;
  2. 创建并暴露路由器:src/router/index.ts,包括:
    • 引入 createRouter 用于创建路由器;
    • 通过 createRouter 参数对象的 history 属性配置路由器工作模式,路由器工作模式有两类:createWebHistory() 和 createWebHashHistory(),本文使用的是 createWebHistory(),两类工作模式间的差异将在其他文章中说明;
    • 通过 createRouter 参数对象的 routes 属性配置路由,每个路由由一个路径 path 和一个组件 component 构成;
    • 暴露路由 export default router。
  3. 引入路由器:src/main.ts,作用是:
    • 全局注册 RouterLink 和 RouterView 组件;
    • 添加全局 $router 和 $route 属性;
    • 启用 useRouter() 和 useRoute() 组合式函数;
    • 触发路由器解析初始路由。
  4. 使用 Vue Router 提供的组件实现路由功能:
    • RouterLink:代替常规的 <a> 标签创建链接,使得能够在不重新加载页面的情况下改变 URL,处理 URL 的生成、编码和其他功能;
    • RouterView:渲染当前 URL 路径对应的页面组件。
相关推荐
程序员大阳6 小时前
副队长Vue3 教程(15)--【练手】列表的搜索过滤与防抖
vue3·过滤·watch·计算属性·防抖·搜索·监听
程序员大阳18 小时前
副队长Vue3教程(12)--计算属性 computed
vue3·computed·计算属性·示例·可读·可写
FRing032 天前
vue3干货总结3(defineOptions(Vue3.3)、defineModel(Vue3.4)、Pinia)
开发语言·前端·vue.js·vue3
千寻技术帮3 天前
基于Spring Boot+Vue的农产品销售管理系统(源码+文档+数据库,附部署教程)
java·数据库·spring boot·后端·mysql·vue
千寻技术帮3 天前
微信小程序血压管理系统设计与实现_50054(Spring Boot + Vue + MySQL)
spring boot·小程序·vue·毕业设计·课程设计·血压助手
FRing033 天前
vue3干货总结2(vue3生命周期、父子通信、模版引用、provide和inject)
开发语言·前端·vue.js·vue3
AI开源软件4 天前
Vue3+SpringBoot3 业务配置统一管理:接管、保存与回退怎样保留有效值
java·前端·vue.js·spring boot·vue·ruoyi office
澹锦汐4 天前
纯前端秋日银杏与枫叶落叶色板智能聚类器:基于 K-Means 色彩量化与直方图自动配方提取
前端·人工智能·ai·语言模型·vue·react·k-means
千寻技术帮4 天前
10454_基于Springboot的企业购销链管理系统
spring boot·mysql·vue·毕业设计·进销存
程序员大阳5 天前
副队长Vue3教程(8)--列表渲染 v-for
javascript·vue3·v-for·列表渲染