17、Vue 配置動態(tài)路由

方式一(動態(tài)路由法):


image.png

步驟:
1、配置動態(tài)路由(main.js)
const routes=[
{path:'/home',component:Home},
{path:'/news',component:News},
{path:'/detail/:id',component:Detail},//動態(tài)匹配路由 /:id
{path:'*',component:Home},
]

image.png

2、在需要動態(tài)跳轉(zhuǎn)的頁面中(Datail.vue)
this.$route.params;//獲取動態(tài)路由的值{id:xx}


image.png

3、配置動態(tài)路由入口(News.vue)
<li v-for="(item,key) in news_list">
<router-link :to="'/detail/'+key">{{key}}--{{item}}</router-link>
</li>


image.png

方式二(get傳值法):
步驟:
1、配置動態(tài)路由(main.js)
const routes=[
{path:'/home',component:Home},
{path:'/news',component:News},
{path:'*',component:Home},//默認跳轉(zhuǎn)路由
{path:'/shop',component:Shop},//和普通的一樣
]
]


image.png

2、在需要動態(tài)跳轉(zhuǎn)的頁面中(Shop.vue)
this.$route.query;//獲取動態(tài)路由的值{id:xx}


image.png

3、配置動態(tài)路由入口(Home.vue)
<ul>
<li v-for="(item,key) in shop_list">
<router-link :to="'/shop?id='+key">{{key}}--{{item}}</router-link>
</li>
</ul>

image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容