Vue實(shí)現(xiàn)各種動(dòng)態(tài)路由生成的技巧分享
一、動(dòng)態(tài)路由的基本玩法
1. 最簡單的動(dòng)態(tài)參數(shù)
// 路由配置
{
path: '/user/:userId',
component: User
}
// 生成鏈接
this.$router.push('/user/' + 我.id)
// 或者
this.$router.push({
name: 'user',
params: { userId: 我.id }
})
我的踩坑提醒:使用params時(shí)一定要用命名路由!
2. 多參數(shù)動(dòng)態(tài)路由
// 配置
{
path: '/article/:category/:id',
component: Article
}
// 生成方式
this.$router.push({
path: `/article/${category}/${articleId}`
})
二、高級(jí)動(dòng)態(tài)路由技巧
1. 正則表達(dá)式約束
{
path: '/user/:userId(\d+)', // 只匹配數(shù)字ID
component: User
}
2. 可選動(dòng)態(tài)參數(shù)
{
path: '/search/:keyword?', // 問號(hào)表示可選
component: Search
}
// 兩種都能匹配
this.$router.push('/search/vue')
this.$router.push('/search')
3. 通配符路由
{
path: '/docs/*', // 匹配/docs下的所有路徑
component: Docs
}
三、動(dòng)態(tài)路由的實(shí)戰(zhàn)應(yīng)用
1. 動(dòng)態(tài)生成側(cè)邊欄菜單
// 菜單配置
const menuItems = [
{ path: '/dashboard', name: '控制臺(tái)' },
{ path: '/user/:id', name: '用戶中心' }
]
// 動(dòng)態(tài)渲染
<router-link
v-for="item in menuItems"
:to="item.path.replace(':id', currentUserId)"
>
{{ item.name }}
</router-link>
2. 面包屑導(dǎo)航生成
computed: {
breadcrumbs() {
const matched = this.$route.matched
return matched.map(route => {
return {
path: this.resolvePath(route),
title: route.meta.title
}
})
}
},
methods: {
resolvePath(route) {
return route.path
.replace(':userId', this.$store.state.userId)
.replace(':projectId', this.currentProject)
}
}
四、動(dòng)態(tài)路由的調(diào)試技巧
1. 查看當(dāng)前路由信息
// 在組件中
console.log(this.$route)
/*
{
path: "/user/123",
params: { userId: "123" },
query: {},
hash: "",
fullPath: "/user/123",
matched: [...]
}
*/
2. 路由匹配測(cè)試工具
// 測(cè)試路徑是否匹配
const match = router.resolve('/user/123')
console.log(match)
五、小楊的動(dòng)態(tài)路由最佳實(shí)踐
- 命名路由:盡量使用name而不是path跳轉(zhuǎn)
- 參數(shù)校驗(yàn):用正則約束動(dòng)態(tài)參數(shù)格式
- 默認(rèn)值:為可選參數(shù)設(shè)置合理的默認(rèn)值
- 文檔注釋:為動(dòng)態(tài)路由添加詳細(xì)注釋
/**
* @name 用戶詳情頁
* @path /user/:userId
* @param {number} userId - 用戶ID必須為數(shù)字
*/
{
path: '/user/:userId(\d+)',
name: 'user',
component: User
}
六、動(dòng)態(tài)路由的常見坑點(diǎn)
1. 刷新后params丟失
解決方案:
// 使用query替代
this.$router.push({
path: '/user',
query: { userId: 我.id } // 變成/user?userId=123
})
2. 動(dòng)態(tài)路由組件不更新
解決方案:
// 監(jiān)聽路由變化
watch: {
'$route.params.userId'(newId) {
this.loadUser(newId)
}
}
寫在最后?
到此這篇關(guān)于Vue實(shí)現(xiàn)各種動(dòng)態(tài)路由生成的技巧分享的文章就介紹到這了,更多相關(guān)Vue動(dòng)態(tài)路由生成內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue在頁面數(shù)據(jù)渲染完成之后的調(diào)用方法
今天小編就為大家分享一篇Vue在頁面數(shù)據(jù)渲染完成之后的調(diào)用方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09
基于Vue3+ts封裝一個(gè)簡單版的Message組件
近日項(xiàng)目中需要使用信息提示框的功能,ui組件庫使用的是字節(jié)的arco-design-vue,看了一下,現(xiàn)有的Message不滿足要是需求,直接使用message組件的話,改樣式太麻煩,所以本文就本就介紹了基于Vue3+ts封裝一個(gè)簡單版的Message組件,需要的朋友可以參考下2023-09-09
基于Vue中使用節(jié)流Lodash throttle詳解
今天小編就為大家分享一篇基于Vue中使用節(jié)流Lodash throttle詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-10-10
如何利用vue3實(shí)現(xiàn)放大鏡效果實(shí)例詳解
最近有項(xiàng)目需要用到對(duì)圖片進(jìn)行局部放大,類似淘寶商品頁的放大鏡效果,經(jīng)過一番研究功能可用,下面這篇文章主要給大家介紹了關(guān)于如何利用vue3實(shí)現(xiàn)放大鏡效果的相關(guān)資料,需要的朋友可以參考下2021-09-09
vue實(shí)現(xiàn)全屏滾動(dòng)效果(非fullpage.js)
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)全屏滾動(dòng)效果,非fullpage.js,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03
Vue Tour封裝實(shí)現(xiàn)統(tǒng)一的頁面引導(dǎo)組件
項(xiàng)目開發(fā)過程中需要實(shí)現(xiàn)用戶引導(dǎo)功能,經(jīng)過調(diào)研發(fā)現(xiàn)一個(gè)好用的 Vue 插件 vue-tour,下面我們就來看看如何基于 vue-tour 封裝一個(gè)統(tǒng)一的引導(dǎo)組件吧2025-05-05
Element-ui 滾動(dòng)條美化的實(shí)現(xiàn)
本文主要介紹了Element-ui 滾動(dòng)條美化的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06

