vue?this.$router和this.$route區(qū)別解析及路由傳參的2種方式?&&?this.$route的各種語法
一 this.$router 和 this. $route 區(qū)別
vue官方文檔說明:
通過注入路由器,我們可以在任何組件內(nèi)通過 this. r o u t e r 訪 問 路 由 器 , 也 可 以 通 過 t h i s . router 訪問路由器,也可以通過 this. router訪問路由器,也可以通過this.route 訪問當(dāng)前路由.
通過打印出: this.$router 和 this. $route :
this.$router(路由實(shí)例) : 是VueRouter的實(shí)例.包含了很多屬性和對(duì)象(比如 history 對(duì)象),任何頁面都可以調(diào)用其 push(), replace(), go() 等方法。
this.$route: 表示當(dāng)前路由對(duì)象,每一個(gè)路由都會(huì)有一個(gè) route 對(duì)象,是一個(gè)局部的對(duì)象,可以獲取對(duì)應(yīng)的 name, path, meta, params, query 等屬性。
1.1 路由實(shí)例的方法
1全局掛載路由實(shí)例
// 全局注冊(cè)的路由 Vue.use(VueRouter)
2 路由實(shí)例方法push
詳見下文: 二 路由傳參的2種方式 (即:路由實(shí)例(this.$router)的push方法)
3 路由實(shí)例方法go
// 頁面路由跳轉(zhuǎn) 前進(jìn)或者后退 this.$router.go(-1) // 后退
4 路由實(shí)例方法replace
//push方法會(huì)向 history 棧添加一個(gè)新的記錄,而replace方法是替換當(dāng)前的頁面, 不會(huì)向 history 棧添加一個(gè)新的記錄 <router-link to="/05" replace>05</router-link> // 一般使用replace來做404頁面 this.$router.replace('/')
二 路由傳參的2種方式 (即:路由實(shí)例(this.$router)的push方法)
1 分為2大類: query 和params 2種方式.
2 每種方式中又分為2類, < router-link to=’…’ > 和 this.$router.push.
3 點(diǎn)擊< router-link :to="…"> 等同于調(diào)用 this. $router.push(…).
2.1 第1類: params (類似post請(qǐng)求 參數(shù)在路徑不可見) 第1種: this. $router.push(…)
傳參&獲取:
傳參: const userId = '123'; //命名的路由 this.$router.push({name:'user', params:{userId}}); //---> /user/123 ***這里的 params 不生效 (重點(diǎn)情況)*** this.$router.push({path:'/user', params:{userId}}); //---> /user 獲取參數(shù): this.$route.params.userId url 形式:url 不帶參數(shù). http:localhost:8080/#/user
總結(jié): params傳參時(shí), 提供path, params將會(huì)被忽略,
push 里面只能是 name:‘xxx’,不能是 path:’/xxx’,
因?yàn)?params 只能用 name 來引入路由,如果這里寫成了 path ,接收參數(shù)頁面會(huì)是: undefined;
第2種: < router-link :to="…">
傳參&獲取:
傳參: <router link :to='/gameInfo/'+uid+"/"+gameid /> //路由中: { path:'/gameInfo/':uid/:gameid name:gameInfo, component:()=>import('./views/gameInfo') } 獲取參數(shù): //像這樣在url中傳入一個(gè)參數(shù),這個(gè)參數(shù)可以是data中的一個(gè)數(shù)據(jù), //也可以是一個(gè)動(dòng)態(tài)的參數(shù),在gameInfo頁面接收參數(shù)的時(shí)候用params接收,比如: this.$route.params.uid //這里的uid是路由中:后邊的參數(shù)
2.2 第2類: query(類似get請(qǐng)求 參數(shù)在路徑可見) 第1種: this. $router.push(…)
傳參&獲取:
傳參: (path 只和 query搭配, path不和params搭配) this.$router.push({path:'/user', query:{userId: '123'}}); 獲取參數(shù): this.$route.query.userId url形式:url中帶參數(shù). http:localhost:8080/#/user?userId=123 獲取參數(shù): this.$route.query.userId
總結(jié):如果提供path, 需要使用 query方式傳參. (path 只和 query搭配, path不和params搭配)
第2種: < router-link :to="…">
傳參: <router link to='/gameInfo?uid='+uid /> 獲取: this.$route.query.uid
注意: 以 / 開頭的嵌套路徑會(huì)被當(dāng)作根路徑。 這讓你充分的使用嵌套組件而無須設(shè)置嵌套的路徑。
2.3 只跳轉(zhuǎn),不傳參數(shù)
// 路徑直接寫:字符串 this.$router.push('home') // 只是跳轉(zhuǎn),不傳參數(shù) //對(duì)象 this.$router.push({path:'home'}) // // 只是跳轉(zhuǎn),不傳參數(shù)
2.4 監(jiān)聽路由
(1)在組件內(nèi),即 this.$route
(2)在 $route 觀察者回調(diào)內(nèi) router.match(location) 的返回值
(3)導(dǎo)航守衛(wèi)的參數(shù):
router.beforeEach((to, from, next) => { // to 和 from 都是 路由信息對(duì)象 }) watch: { $route(to, from) { // to 和 from 都是 路由信息對(duì)象 } }
三 this.$route的各種語法
this. $route.query
this. $route.params
this. $route.path
this. $route.matched
<router-link class="icon-go" :to="{ name: '首頁'}" v-if="$route.matched[0].path=='/category'"> </router-link> <span class="icon-go" @click="$router.go(-1)" v-else></span> <slot name="title"></slot>
$route.path
類型: string
字符串,對(duì)應(yīng)當(dāng)前路由的路徑,總是解析為絕對(duì)路徑,如 /foo/bar。
$route.params
類型: Object
一個(gè) key/value 對(duì)象,包含了動(dòng)態(tài)片段和全匹配片段,如果沒有路由參數(shù),就是一個(gè)空對(duì)象。
$route.query
類型: Object
一個(gè)key/value 對(duì)象,表示 URL 查詢參數(shù)。例如,對(duì)于路徑/foo?user=1,則有 $route.query.user == 1,如果沒有查詢參數(shù),則是個(gè)空對(duì)象。
$route.matched
類型:Array
一個(gè)數(shù)組,包含當(dāng)前路由的所有嵌套路徑片段的路由記錄 。路由記錄就是 routes 配置數(shù)組中的對(duì)象副本 (還有在 children 數(shù)組)。
const router = new VueRouter({ routes: [ // 下面的對(duì)象就是路由記錄 { path: '/foo', component: Foo, children: [ // 這也是個(gè)路由記錄 { path: 'bar', component: Bar } ] } ] })
當(dāng) URL 為 /foo/bar,$route.matched 將會(huì)是一個(gè)包含從上到下的所有對(duì)象 (副本)。
$route.fullPath
路由是:/path/:type真正路徑是:/path/list
path匹配路徑: /path/list
fullPath匹配路由: /path/:type
$route.meta (路由原信息meta)
const router = new VueRouter({ routes: [ { path: '/foo', component: Foo, children: [ { path: 'bar', component: Bar, // a meta field meta: { requiresAuth: true ,keepAlive:true}//1.權(quán)限 2.內(nèi)存緩存,單頁面切換 } ] } ] })
先理解什么是路由記錄 : 路由記錄就是 routes 配置數(shù)組中的對(duì)象副本 (還有在 children 數(shù)組)。
上方代碼中的路由記錄見下方:
//一級(jí)路由 { path: '/foo', component: Foo, children: [ { path: 'bar', component: Bar, // a meta field meta: { requiresAuth: true ,keepAlive:true}//1.權(quán)限 2.內(nèi)存緩存,單頁面切換 } ] } //一級(jí)路由的子路由 { path: 'bar',component: Bar,meta: { requiresAuth: true ,keepAlive:true } } //兩者都是 路由記錄
定義路由的時(shí)候可以配置 meta 字段
meta這個(gè)對(duì)象中的屬性可以自由定義 ,
取的時(shí)候就用 this. $route.meta.屬性名
根據(jù)上面的路由配置,/foo/bar 這個(gè) URL 將會(huì)匹配父路由記錄以及子路由記錄
一個(gè)路由匹配到的所有路由記錄會(huì)暴露為 $route 對(duì)象 (還有在導(dǎo)航守衛(wèi)中的路由對(duì)象) 的 $route.matched 數(shù)組。
檢查路由記錄中的 meta 字段 ,我們需要遍歷 $route.matched
$route.matched
一個(gè)數(shù)組,包含當(dāng)前路由的所有嵌套路徑片段的路由記錄
一個(gè)路由匹配到的所有路由記錄會(huì)暴露為 $route 對(duì)象 (還有在導(dǎo)航守衛(wèi)中的路由對(duì)象) 的 $route.matched 數(shù)組
路由元信息 .meta $route.matched 搭配路由守衛(wèi) 進(jìn)行驗(yàn)證
router.beforeEach((to, from, next) => { if (to.matched.some(record => record.meta.requiresAuth)) { // this route requires auth, check if logged in // if not, redirect to login page. if (!auth.loggedIn()) { next({ path: '/login', query: { redirect: to.fullPath } }) } else { next() } } else { next() // 確保一定要調(diào)用 next() } })
到此這篇關(guān)于vue this.$router和this.$route區(qū)別解析及路由傳參的2種方式 && this.$route的各種語法的文章就介紹到這了,更多相關(guān)vue this.$router 與 this.$route 區(qū)別內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue使用正則校驗(yàn)文本框?yàn)檎麛?shù)
這篇文章主要介紹了Vue使用正則校驗(yàn)文本框?yàn)檎麛?shù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10Vue父組件觸發(fā)子組件中的實(shí)現(xiàn)方法
文章總結(jié):介紹了兩種實(shí)現(xiàn)父組件觸發(fā)子組件方法的常用方法:通過ref訪問子組件實(shí)例并調(diào)用方法,以及使用自定義事件觸發(fā)子組件方法2025-01-01詳解vue中的動(dòng)態(tài)組件component和keep-alive
這篇文章主要介紹了詳解vue中的動(dòng)態(tài)組件component和keep-alive的相關(guān)資料,這大家需要注意include屬性和exclude屬性只能用一個(gè),不能同時(shí)使用,本文給大家介紹的非常詳細(xì),需要的朋友參考下吧2023-11-11Vue ElementUI this.$confirm async await封
這篇文章主要介紹了Vue ElementUI this.$confirm async await封裝方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09elementUI給el-tabs/el-tab-pane添加圖標(biāo)效果實(shí)例
這篇文章主要給大家介紹了關(guān)于elementUI給el-tabs/el-tab-pane添加圖標(biāo)效果實(shí)例的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用elementUI具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-07-07