Vue實(shí)現(xiàn)各種動(dòng)態(tài)路由生成的技巧分享
一、動(dòng)態(tài)路由的基本玩法
1. 最簡(jiǎn)單的動(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?', // 問(wèn)號(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 用戶詳情頁(yè)
* @path /user/:userId
* @param {number} userId - 用戶ID必須為數(shù)字
*/
{
path: '/user/:userId(\d+)',
name: 'user',
component: User
}
六、動(dòng)態(tài)路由的常見(jiàn)坑點(diǎn)
1. 刷新后params丟失
解決方案:
// 使用query替代
this.$router.push({
path: '/user',
query: { userId: 我.id } // 變成/user?userId=123
})
2. 動(dòng)態(tài)路由組件不更新
解決方案:
// 監(jiān)聽(tīng)路由變化
watch: {
'$route.params.userId'(newId) {
this.loadUser(newId)
}
}
寫(xiě)在最后?
到此這篇關(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 和 iView分片上傳功能實(shí)現(xiàn)(上傳組件)
本文介紹了基于Vue和iView的文件分片上傳技術(shù),通過(guò)將文件拆分成多個(gè)小塊并逐塊上傳,解決了大文件上傳時(shí)的諸多問(wèn)題,如上傳速度慢、超時(shí)和網(wǎng)絡(luò)中斷等,它還展示了如何實(shí)現(xiàn)分片上傳的進(jìn)度顯示、錯(cuò)誤處理和斷點(diǎn)續(xù)傳等功能,感興趣的朋友跟隨小編一起看看吧2025-01-01
VSCode寫(xiě)vue項(xiàng)目一鍵生成.vue模版,修改定義其他模板的方法
這篇文章主要介紹了VSCode寫(xiě)vue項(xiàng)目一鍵生成.vue模版,修改定義其他模板的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Vue2+element-ui實(shí)現(xiàn)面包屑導(dǎo)航
這篇文章主要為大家詳細(xì)介紹了Vue2+element-ui使用面包屑導(dǎo)航的正確姿勢(shì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
基于mpvue搭建微信小程序項(xiàng)目框架的教程詳解
mpvue從底層支持 Vue.js 語(yǔ)法和構(gòu)建工具體系,同時(shí)再結(jié)合相關(guān)UI組件庫(kù),便可以高效的實(shí)現(xiàn)小程序開(kāi)發(fā)。這篇文章主要介紹了基于mpvue搭建小程序項(xiàng)目框架 ,需要的朋友可以參考下2019-04-04
vue實(shí)現(xiàn)簡(jiǎn)單分頁(yè)功能
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)簡(jiǎn)單分頁(yè)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
vue實(shí)現(xiàn)商品加減計(jì)算總價(jià)的實(shí)例代碼
這篇文章主要介紹了vue實(shí)現(xiàn)商品加減計(jì)算總價(jià)的實(shí)例代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08

