Vue中路由的使用

目录

[1 作用](#1 作用)

[2 使用方法](#2 使用方法)

[2.1 安装路由](#2.1 安装路由)

[2.2 创建路由并导出](#2.2 创建路由并导出)

[2.3 在应用实例中使用vue-router](#2.3 在应用实例中使用vue-router)

[2.4 声明router-view,展示组件内容](#2.4 声明router-view,展示组件内容)

[2.5 页面跳转](#2.5 页面跳转)

[3 补充内容-子路由](#3 补充内容-子路由)


1 作用

能够按不同的访问路径,展示不同组件的内容。

2 使用方法

2.1 安装路由

在项目的终端或者路径下的命令提示符窗口中,写入以下命令(其中的4是指版本为4):

npm install vue-router@4

2.2 创建路由并导出

①在src目录下创建一个router文件夹,再在其中创建index.js文件

注:当然,其它名字.js也可以,只不过2.3的导入需要额外书写内容

②向index.js中书写以下格式内容:

javascript 复制代码
//引入依赖
import { createRouter, createWebHistory } from 'vue-router'

//导入组件
//xx和yy可以替换为实际的内容
import XxVue from '@/views/Xx.vue'
import YyVue from '@/views/Yy.vue'

//定义路由关系
const routes = [
    //这样就可以通过http://localhost:5173/Xx访问Xx.vue的内容了
    { path: '/Xx', component: XxVue},
    //同理,可以访问Yy.vue。并将其作为默认页面
    { path: '/', component: YyVue}
]

//定义路由
const router = createRouter({
    //history是路由模式,还有一种哈希方式(createWebHashHistory),配置方式自行探索
    history: createWebHistory(),
    routes: routes
})

//导出路由
export default router

2.3 在应用实例中使用vue-router

在main.js中引入如下内容:

javascript 复制代码
//这里只展示主要内容,其它内容...
import { createApp } from 'vue'
import App from './App.vue'

//导入路由,默认导入index.js
//如果不是index.js,而是xx.js,则from '@/router/xx.js'
import router from '@/router'

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

2.4 声明router-view,展示组件内容

在App.vue中的<template>内添加如下,进去的默认页面就是Yy.vue了:

XML 复制代码
<template>
    <router-view></router-view>
</template>

2.5 页面跳转

如果想在某个函数执行之后,想跳转某个页面,可以如下使用:

①在该函数所在的页面导入,如xx.vue:

javascript 复制代码
import {useRouter} from 'vue-router'

②找到该函数,并向其中添加如下内容:

javascript 复制代码
const xx = async() => {
    //其它内容...
    //router.push跳转到指定页面,这里是默认页面
    router.push('/')
}

3 补充内容-子路由

如下图所示,App.vue使用的叫一级路由,X.vue使用的就属于二级路由(X->Z,X->H),其中二级路由就可以称为一级路由的子路由,:

如element-ui中的例子所示,要在当前页面访问的Option1就属于子路由:

配置如下,在index.js中新增一些内容:

javascript 复制代码
//引入依赖
import { createRouter, createWebHistory } from 'vue-router'

//导入组件
//xx和yy可以替换为实际的内容
import XxVue from '@/views/Xx.vue'
import YyVue from '@/views/Yy.vue'
import ZzVue from '@/views/Zz.vue'
import HhVue from '@/views/Hh.vue'

//定义路由关系
const routes = [
    //这样就可以通过http://localhost:5173/Xx访问Xx.vue的内容了
    { path: '/Xx', component: XxVue},
    //同理,可以访问Yy.vue。并将其作为默认页面
    //redirect:'/func/xyz'是将Zz.vue页面作为进入'/'默认的访问的页面
    //children:子路由
    { path: '/', component: YyVue,redirect:'/func/xyz', children:[
        { path: '/func/xyz', component: ZzVue},
        { path: '/func/zxy', component: HhVue},
    ]}
]

//定义路由
const router = createRouter({
    //history是路由模式,还有一种哈希方式(createWebHashHistory),配置方式自行探索
    history: createWebHistory(),
    routes: routes
})

//导出路由
export default router
相关推荐
无所谓^_^1 天前
个人健康管理系统 | Springboot3 | Vue3
java·vue·毕业设计·springboot·课程设计·毕设·前后端分离
在努力的前端小白2 天前
基于FastAPI+LangChain+LangGraph+Agent+多模态的AI Agent 有渔渔具网站
人工智能·langchain·vue·fastapi
sun_weitao2 天前
用 FastAPI + MQTT 做一个 ESP32最小化跑通示例
中间件·vue·fastapi
汤米粥6 天前
2026年主流前端技术框架
vue·react·elementplus·tailwind css·angular‌·svelte‌·bootstrap‌
codigger6 天前
Vue nextTick 到底在等什么?一篇讲透原理与那些年踩过的坑
程序员·vue·编程·异步更新·#人工智能
咚咚咚小柒7 天前
【前端】LangChain.js学习
前端·javascript·人工智能·typescript·langchain·vue·ai编程
悟道书生9 天前
若依 RBAC 权限控制学习笔记
java·网络·笔记·sql·学习·vue
mewowaBc9 天前
使用Pinia持久化存储实现用户认证系统
vue·pinia·用户认证系统
gc_229910 天前
使用VS Code终端创建Vue项目报错:“npm : 无法加载文件 ...\npm.ps1,因为在此系统上禁止运行脚本”
vue·vs code
小阿鑫10 天前
尤雨溪 : Vue 4 已经在规划中了!
vue·vite·vite+·程序员海军·vue4