Logo

Logo

Vue3的vue-router路由详解

Pastore Antonio
Pastore Antonio 2023年10月23日
522 阅读 0 评论 约 2528 字 阅读约 6 分钟

这篇文章是接着【三分钟快速搭建Vue3+webpack项目】的内容做的开发,有基础的可以跳过 【三分钟快速搭建Vue3+webpack项目】,直接看以下的内容。

Vue3的vue-router路由详解:

首先安装路由依赖模块:

npm install vue-router@4

所需代码文件如下图:
图1

aac4421b71344dc0931439f78dc83889.png

 

所需要的主要文件:index.html、index.js、App.vue

index.html:模板页面,项目中的代码编译之后都是放入到模板页面中id为app的元素这种。
代码如下:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>Vue</title>
  </head>
  <body>
    <div id="app"></div>
  </body>
</html>

index.js:创建一个vue应用,将路由放入应用之中,并挂载到模板页面id为app的元素上。
代码如下:

import { createApp } from 'vue'
import { createRouter,createWebHashHistory } from 'vue-router'
import App from './App.vue'

// 1. 定义路由组件:这里直接用的对象数据,也可以导入其他组件。
const Main = { render(){ return '月影WEB 欢迎大家来学习各种技术知识!'} }
const Lists = { render(){ return '月影WEB-列表页面'} }
const Details = { render(){ return '月影WEB-详情页面'} }

// 2. 定义一些路由:每个路由都需要映射到一个组件。
const routes = [
    { path: '/', component: Main },
    { path: '/lists', component: Lists },
    { path: '/details', component: Details },
]

// 3. 创建路由实例并传递 `routes` 配置。
const router = createRouter({
    // 内部提供了 history 模式的实现。为了简单起见,我们在这里使用 hash 模式。
    history: createWebHashHistory(),
    routes, // `routes: routes` 的缩写
})

// 4.创建一个vue应用,将App组件和定义的路由放入到vue应用,并挂载到模板页面id为app的元素上。
createApp(App).use(router).mount('#app')

1. 定义路由组件:这里直接用的对象数据,也可以导入其他组件。
const Main = { render(){ return ‘月影WEB 欢迎大家来学习各种技术知识!’} }
const Lists = { render(){ return ‘月影WEB-列表页面’} }
const Details = { render(){ return ‘月影WEB-详情页面’} }

注意:Main、Lists 、Details 定义了三个路由组件,return后面就是每个路由组件展示的UI。

 

2. 定义一些路由:每个路由都需要映射到一个组件。
const routes = [
    { path: ‘/’, component: Main },
    { path: ‘/lists’, component: Lists },
    { path: ‘/details’, component: Details },
]

注意:path是路由路径,也是地址栏会显示的路径,component是放路由组件的,每个路由路径都可以进行组件映射。

 

3. 创建路由实例并传递 `routes` 配置。
const router = createRouter({
    // 内部提供了 history 模式的实现。为了简单起见,我们在这里使用 hash 模式。
    history: createWebHashHistory(),
    routes, // `routes: routes` 的缩写
})

注意:这里只做了简单的配置,history是路由的模式,routes放定义的路由,createRouter创建路由实例。

 

4.创建一个vue应用,将App组件和定义的路由放入到vue应用,并挂载到模板页面id为app的元素上。
createApp(App).use(router).mount(‘#app’)

 

App.vue:用来展示不同的路由页面UI。
代码如下:

<template>
    <router-view></router-view>
</template>
<script>
export default {

}
</script>

注意:<router-view>是用来展示路由对应的组件UI的。
 

启动服务的效果如下:

{ path: ‘/’, component: Main }

9f71b5c6890e472c820b3d0418326606.png

 

{ path: ‘/lists’, component: Lists }

f794eb83457c492785dc9c171e5473cd.png

{ path: ‘/details’, component: Details }

 99e69e11e50548ee8174da264b74c0ab.png

 

关注公众号(月影WEB),了解更多的前后端知识;
欢迎大家关注互相交流学习;

 

查看完整代码

橙子主题打折出售

其实我不卖,主要是这里是放广告的,所以就放了一个
毕竟主题都没做完,卖了也是坑.

购买它

附件下载

共 4 个文件
aac4421b71344dc0931439f78dc83889.png
PNG 28.6 KB
9f71b5c6890e472c820b3d0418326606.png
PNG 18.3 KB
f794eb83457c492785dc9c171e5473cd.png
PNG 17.3 KB
99e69e11e50548ee8174da264b74c0ab.png
PNG 15.7 KB
部分文章可能存在转载,如果涉及到侵权,请联系删除文章。

探索AIGC相关的精彩内容,共 15 篇文章

Azure AI 服务之语音识别

简介 Azure AI 服务中的语音识别 API 是微软提供的一项先进技术,旨在帮助开发者轻松实现语 ... Vue3的vue-router路由详解

2026-02-17 · Xzavier Aaron
MCP | 一文详解什么是 MCP以及 MCP 可以做什么

一、什么是 MCP MCP(Model Context Protocol)是一个专为大型语言模型(L ... Vue3的vue-router路由详解

2026-02-14 · Shen, Luke
你的工作流程,值得一个“全自动数字分身”:录制、截图、成文,一气呵成

一、一句话认识 TestFlow Recorder 在数字化工作环境中,如何准确记录操作步骤并生成清 ... Vue3的vue-router路由详解

2026-02-14 · Xzavier Aaron
Flowise 前端框架配置指南

用户需求 问题:有没有适合配置 Flowise 的前端框架? 目标:寻找类似 Open WebUI ... Vue3的vue-router路由详解

2026-02-14 · Xzavier Aaron