Vue3学习与实战:配置使用vue-router路由
创作时间:
作者:
@小白创作中心
Vue3学习与实战:配置使用vue-router路由
引用
CSDN
1.
https://m.blog.csdn.net/qq_24428851/article/details/142793983
随着Vue版本的升级,Vue 2.x项目和Vue 3.x项目在使用vue-router上有些区别,本文就简单介绍下vue-router在Vue3中的配置和使用。
一、目录结构
demo/
package.json
vite.config.js
index.html
public/
src/
api/
assets/
common/
components/
store/
views/
home.vue
list.vue
router/
index.js
App.vue
main.js
二、版本依赖
- vite: ^2.0.0
- vue: ^3.2.8
- vue-router: ^4.0.1
三、配置路由
3-1.配置src/router/index.js路由文件
// src/router/index.js
import {createRouter, createWebHistory, createWebHashHistory} from 'vue-router'
import { defineAsyncComponent } from 'vue'
const router = createRouter({
// history: createWebHashHistory(), // hash 模式
history: createWebHistory(), // history 模式
routes: [
{
path: '/',
name: 'home',
component: defineAsyncComponent(() => import(`../views/home.vue`)),
meta: {
title: '首页',
},
},
{
path: '/list',
name: 'list',
component: defineAsyncComponent(() => import(`../views/list.vue`)),
meta: {
title: '列表页',
},
},
{
path: '/*',
redirect: '/',
},
]
})
// 全局路由守卫
router.beforeEach((to, from, next)=>{
// console.log(to, from)
if (to.meta.title) {
document.title = `${to.meta.title}`;
}
next()
})
router.afterEach((to, from)=>{
// console.log(to, from)
console.log('afterEach')
})
export default router
说明:
- 路由模式:
- history模式对应createWebHistory()方法
- hash模式对应createWebHashHistory()方法
- 路由懒加载:在vite+Vue3项目中使用import()会有报错,所以使用vue提供的一个方法defineAsyncComponent,详见另一篇:vue3 + vite实现异步组件和路由懒加载
3-2.在src/main.js入口文件中注册使用路由
// src/main.js
import { createApp } from 'vue'
import router from './router'
import store from './store'
import App from './App.vue'
const app = createApp(App)
app.use(router).use(store);
app.mount('#app')
3-3.在src/App.vue文件中使用
// src/App.vue
<template>
<router-view/>
</template>
四、使用路由
4-1.在Option API中使用和Vue 2.x中使用没有差别。如下:
<template>
<div></div>
</template>
<script>
export default {
data() {
return {}
},
mounted() {
// 路由跳转 && 设置参数
this.$router.push({
path: '/list',
query: {
id: 123,
},
})
// 获取参数
let { id } = this.$route.query
},
}
</script>
4-2.在Composition API中不能再直接访问this.$router或this.$route,所以要使用useRouter和useRoute函数。
<template>
<div></div>
</template>
<script>
import { ref } from 'vue'
import { useRouter, useRoute, onBeforeRouteLeave, onBeforeRouteUpdate } from 'vue-router'
import ajax from "./ajax";
export default {
setup () {
const router = useRouter()
const route = useRoute()
// 路由跳转 && 设置参数
router.push({
path: '/list',
query: {
id: 123,
},
})
// 获取参数
let { id } = route.query
// 局部路由守卫
onBeforeRouteLeave((to, from) => {
const answer = window.confirm(
'是否要离开本页面?'
)
// 取消导航并停留在同一页面上
if (!answer) return false
})
const userData = ref()
onBeforeRouteUpdate(async (to, from) => {
//仅当 id 更改时才获取用户,例如仅 query 或 hash 值已更改
if (to.params.id !== from.params.id) {
userData.value = await ajax(to.params.id)
}
})
},
}
</script>
热门推荐
汉字“三”的书写技巧与文化意义
什么是西藏天珠?佩戴天珠的禁忌是什么?
今天你“搭”了吗?“搭子”社交现象观察
底商是什么意思
冬季室内取暖新方案:告别空调暖气,巧用这些小妙招让房间暖烘烘
一根玉米的热量、营养成分?吃玉米的功效、好处大公开
电流控制器怎么调节
20岁生娃好VS30岁生娃好?优劣势对比来喽
小米拍月亮设置参数:如何开启超级月亮模式及调整参数
开放性骨折的急救措施
复旦“90后”超帅青年博导高悦联手彭慧胜院士,最新Nature!
吃水果不是越多越好!超实用吃水果指南来了,这样吃更健康→
零存整取存款方式的利息计算及优缺点分析
被教育部明令禁止的“升学捷径”,帮家长和孩子识别
往复式压缩机节能降耗途径研究
豫园:繁华都市中的江南园林宝藏!
如何在需求梳理中处理客户或用户的期望
乡饮酒礼:尊老敬贤的千年礼俗
单位直招合同工待遇揭秘:薪资及福利全解析
追溯茅台历史渊源,率先提出“茅台酒”概念的烧坊是偈盛烧坊?
药师提醒:不正确使用药物也会伤牙,这些药物需警惕
如何关闭汽车的GPS功能?这种操作有哪些步骤和隐私保护建议?
红烧梅花肉
怀宁县民间文艺产业传承发展巡礼
一滴水的体积:标准与影响因素
霍林郭勒市:职业技能培训拓宽群众“就业路”
泰国旅游提示:被要求拍照背后的法律风险
在夏天,突然读懂了《红楼梦》里的这些细节
三餐吃得好,恢复没烦恼——肝损伤患者饮食指南
如何看待重庆、成都、西安这3座城市?2035年的城市定位是什么?