詳解vue-router 2.0 常用基礎(chǔ)知識點之router-link
前端采用前后端分離的方式進行開發(fā),我們使用vue2.0框架,做單頁面應(yīng)用難免會用到vue-router,今天把項目中的用到router-link摘出來,一是想整理一下這些用法,方便下次快速查找,二是重新再過一下vue-router,增加熟悉度。也希望下面這些例子能幫到其他使用vue-router的朋友。
1,$route.params
類型: Object
一個 key/value 對象,包含了 動態(tài)片段 和 全匹配片段,如果沒有路由參數(shù),就是一個空對象。
path: '/detail/:id' 動態(tài)路徑參數(shù) 以冒號開頭
const routes = [ {path: '/detail/:id', component: Detail, name: 'detail', meta: {title: ''}}, {path: '/activity', component: Activity, name: 'activity', meta: {isNeedAuth: false, title: '活動現(xiàn)場'}}, ];
還可以在一個路由中設(shè)置多段『路徑參數(shù)』,對應(yīng)的值都會設(shè)置到 $route.params 中
1個參數(shù)
模式: /user/:username
匹配路徑: /user/evan
$route.params:{ username: 'evan' }
多個參數(shù)
模式: /user/:username/post/:post_id
匹配路徑:/user/evan/post/123
$route.params:{ username: 'evan', post_id: 123 }
復(fù)用組件時,想對路由參數(shù)的變化作出響應(yīng)的話,你可以簡單地 watch(監(jiān)測變化) $route 對象:
const User = { template: '...', watch: { '$route' (to, from) { // 對路由變化作出響應(yīng)... } } }
或者像下面這樣,只要$route發(fā)生變化,就可以做某事:
export default { data () { return {} }, watch: { // 如果路由有變化,會再次執(zhí)行該方法 '$route': 'doSomeThing' }, methods: { doSomeThing(){} } }
綜合案例
// 當匹配到一個路由時,參數(shù)值會被設(shè)置到 this.$route.params,可以在每個組件內(nèi)使用。 // 可以通過this.$route.params.id來取上動態(tài)的id <router-link :to="{path: '/detail/' + this.$route.params.id}" > 此團詳情 </router-link> // 還可以用命名路由的方式: <router-link :to="{ name: 'detail', params:{ id: this.$route.params.id }}" > 此團詳情 </router-link> // 還可以用router.push()的方式 router.push({name:'detail', params: { id: this.$route.params.id}}) // 以上三種方式都可以實現(xiàn)跳轉(zhuǎn),都可以通過this.$route.params來取到參數(shù)
2,$route.query
類型: Object
一個 key/value 對象,表示 URL 查詢參數(shù)。例如,對于路徑 /foo?user=1,則有 $route.query.user == 1,如果沒有查詢參數(shù),則是個空對象。
// 動態(tài)數(shù)據(jù)的查詢參數(shù) export default { data() { return { queryData: {} } }, created() { this.$http.get(url) .then(function (response) { // ... if (data.code == 0) { this.queryData.order_id = data.content.order_id; this.queryData.business_id = data.content.business_id; this.queryData.coupon_id = data.content.coupons.coupon_id; } // ... }, function (response) { // ... }) }, } // 使用 <router-link :to="{ path: '/backend/verify_coupon', query:this.queryData }">驗證抵扣券</router-link>
3,定義路由的時候可以配置 meta 字段
// 舉個例子 const routes = [ {path: '/activity', component: Activity, name: 'activity', meta: {isNeedAuth: false, title: '活動現(xiàn)場'}}, ];
實際工作中使用的時候就可以像下面這樣:
import { setTitleHack } from './utils'; import Activity from './views/activity.vue' import Start from './views/start.vue' // 定義路由的時候在meta中加入自定義字段 const routes = [ {path: '/activity', component: Activity, name: 'activity', meta: {isNeedAuth: false, title: '活動現(xiàn)場'}}, {path: '/start', component: Start, name: 'start', meta: {isNeedAuth: false, title: '活動現(xiàn)場'}}, ]; const router = new VueRouter({...}); router.beforeEach((to, from, next) => { // 動態(tài)修改頁面的title setTitleHack(to.meta.title); // 根據(jù)自定義的路由元信息來做判斷: if (to.meta.isNeedAuth !== false) { // do something } else { // do something } next(); });
4,append
設(shè)置 append 屬性后,則在當前(相對)路徑前添加基路徑。例如,我們從 /a 導(dǎo)航到一個相對路徑 b,如果沒有配置 append,則路徑為 /b,如果配了,則為 /a/b
<router-link :to="{path:'/coupon/detail/'+item.order_id, append:'true'}"></router-link>
如果上面這個路由是從home頁面跳轉(zhuǎn)過來,得到的結(jié)果就是/home/coupon/detail/id
5,active-class
類型: string
默認值: "router-link-active"
設(shè)置 鏈接激活時使用的 CSS 類名。默認值可以通過路由的構(gòu)造選項 linkActiveClass 來全局配置。
<router-link tag="li" :to="{path:'/home', activeClass: 'bottom-nav-active'}"></router-link>
7,綜合案例
// 命名路由,append 屬性,查詢參數(shù),router-link渲染成<li>標簽 <router-link tag="li" :to="{name:'demandindex', append:true, query: {isFormBackend: 1}, activeClass: 'bottom-nav-active'}"> </router-link> // to的值:字符串形式 <router-link to="banner.image_url" ></router-link> // to的值:對象形式,拼接多個動態(tài)參數(shù) <router-link :to="{path: '/my/modify?modify=fullname&val=' + profile.nickname}" ></router-link> // to的值:對象形式 <router-link :to="{path: '/home'}">返回首頁</router-link> // to的值:對象形式,拼接動態(tài)參數(shù) <router-link to="{path: '/backend/coupon_order_detail/' + product.order_id+'?order_status='+product.order_status}"></router-link> // to的值:對象形式,帶一個路徑參數(shù) <router-link :to="{path: '/detail/' + this.$route.params.id}" ></router-link>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue3?父控件遠程獲取數(shù)據(jù)在子組件上顯示不出來的解決方案
這篇文章主要介紹了vue3?父控件遠程獲取數(shù)據(jù),在子組件上顯示不出來,本文給大家分享兩種解決方案幫助大家解決這個問題,需要的朋友可以參考下2023-08-08vue+springboot用戶注銷功能實現(xiàn)代碼
這篇文章主要介紹了vue+springboot用戶注銷功能,本文通過示例代碼給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧2024-05-05Vue組件傳值過程中丟失數(shù)據(jù)的分析與解決方案
這篇文章主要給大家介紹了關(guān)于Vue組件傳值過程中丟失數(shù)據(jù)的分析與解決方案,文中通過圖文介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03Vue?ElementUI在el-table中使用el-popover問題
這篇文章主要介紹了Vue?ElementUI在el-table中使用el-popover問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04