淺談vue-router 路由傳參的方法
路由傳參數(shù)。在很多時(shí)候我們需要路由上面?zhèn)鬟f參數(shù),比如新聞列表頁(yè),我們需要傳遞新聞ID,給新聞詳細(xì)頁(yè)。
1.新聞列表頁(yè)模板
<template id="news"> <div> <h2>新聞列表</h2> <ul> <li> <router-link to="/news/001">新聞001</router-link> </li> <li> <router-link to="/news/002">新聞002</router-link> </li> </ul> </div> </template>
我們?cè)L問/news/001,跳轉(zhuǎn)到新聞詳細(xì)頁(yè),展示001的這條新聞。
2.新聞詳細(xì)頁(yè)組件準(zhǔn)備
<template id="NewsDetail"> <div> 新聞詳細(xì)頁(yè)面 <span>{{$route.params.id}}</span> </div> </template>
$route.params.id獲取路由上的參數(shù)
3.定義路由,增加一個(gè)路由
{ path: '/news/:id', component: NewsDetail },
4.全部代碼如下:
<!DOCTYPE html> <html> <head> <title></title> <meta charset="utf-8"> <script src="http://unpkg.com/vue/dist/vue.js"></script> <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script> </head> <body> <div id="box"> <p> <router-link to="/home">home</router-link> <router-link to="/news">news</router-link> </p> <router-view></router-view> </div> <!-- 模板抽離出來 --> <template id="home"> <!-- 注意:組件只能有一個(gè)根元素,所以我們包裝到這個(gè)div中 --> <div> <h2>首頁(yè)</h2> <router-link to="/home/login">登錄</router-link> <router-link to="/home/reg">注冊(cè)</router-link> <!-- 路由匹配到的組件將渲染在這里 --> <router-view></router-view> </div> </template> <template id="news"> <div> <h2>新聞列表</h2> <ul> <li> <router-link to="/news/001">新聞001</router-link> </li> <li> <router-link to="/news/002">新聞002</router-link> </li> </ul> </div> </template> <template id="login"> <div>登錄界面</div> </template> <template id="reg"> <div>注冊(cè)界面</div> </template> <template id="NewsDetail"> <div> 新聞詳細(xì)頁(yè)面 <span>{{$route.params.id}}</span> </div> </template> <script type="text/javascript"> // 1. 定義(路由)組件。 const Home = { template: '#home' }; const News = { template: '#news' }; const Login = { template: '#login' }; const Reg = { template: '#reg' }; //新聞詳細(xì)頁(yè)組件 const NewsDetail = { template: '#NewsDetail' }; // 2. 定義路由 const routes = [ { path: '/', redirect: '/home' }, { path: '/home', component: Home, children:[ { path: '/home/login', component: Login}, { path: '/home/reg', component: Reg} ] }, { path: '/news', component: News,}, { path: '/news/:id', component: NewsDetail }, ] // 3. 創(chuàng)建 router 實(shí)例,然后傳 `routes` 配置 const router = new VueRouter({ routes // (縮寫)相當(dāng)于 routes: routes }) // 4. 創(chuàng)建和掛載根實(shí)例。 // 記得要通過 router 配置參數(shù)注入路由, // 從而讓整個(gè)應(yīng)用都有路由功能 const app = new Vue({ router }).$mount('#box') // 現(xiàn)在,應(yīng)用已經(jīng)啟動(dòng)了! </script> </body> </html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue調(diào)用谷歌授權(quán)登錄獲取用戶通訊錄的實(shí)現(xiàn)示例
本文主要介紹了vue調(diào)用谷歌授權(quán)登錄獲取用戶通訊錄的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07Vue Render函數(shù)原理及代碼實(shí)例解析
這篇文章主要介紹了Vue Render函數(shù)原理及代碼實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07Vue3表單組件el-form校驗(yàn)規(guī)則rules屬性示例詳解
在el-form中正確使用rules校驗(yàn)是非常重要的,rules使用不當(dāng)容易出現(xiàn)規(guī)則不生效、規(guī)則一直被觸發(fā)等各種現(xiàn)象,這篇文章主要給大家介紹了關(guān)于Vue3表單組件el-form校驗(yàn)規(guī)則rules屬性的相關(guān)資料,需要的朋友可以參考下2024-08-08優(yōu)化Vue template中大量條件選擇v-if的方案分享
本文我們將給大家詳細(xì)的講解一下如何優(yōu)化Vue template 中的大量條件選擇v-if,文中通過代碼示例介紹的非常詳細(xì),有詳細(xì)的優(yōu)化方案,感興趣的朋友可以參考閱讀下2023-07-07Element中Upload組件上傳功能實(shí)現(xiàn)(圖片和文件的默認(rèn)上傳及自定義上傳)
這篇文章主要介紹了Element中Upload組件上傳功能實(shí)現(xiàn)包括圖片和文件的默認(rèn)上傳及自定義上傳,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-01-01