關(guān)于vue-router路由的傳參方式params query
更新時間:2023年10月08日 08:49:14 作者:Morning_xx
這篇文章主要介紹了關(guān)于vue-router路由的傳參方式params query,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
vue-router路由的傳參方式params query
1.$router 和 $route的區(qū)別
- $router : 路由操作對象、操作路由、包括路由的跳轉(zhuǎn)方法,鉤子函數(shù)等、只寫對象;
- $route : 路由信息對象、獲取路由信息、包括path,params,hash,query,fullPath,matched,name等路由信息參數(shù)、只讀對象;
//操作 路由跳轉(zhuǎn) this.$router.push({ name:'hello', params:{ name:'word', age:'11' } }) //讀取 路由參數(shù)接收 this.name = this.$route.params.name; this.age = this.$route.param.age;
2.query傳參
- path配合
- 刷新頁面參數(shù)不丟失
//query傳參,使用path跳轉(zhuǎn) this.$router.push({ path:'second', query:{ queryId:'20230129', queryName:'Emma' } )} //query傳參接受 this.quertId = ths.$route.query.queryId this.queryName = this.$route.query.queryName
3.params傳參
- 和name配合
- 刷新頁面參數(shù)會丟失
//params傳參,使用name this.$router.push({ name:'second', params:{ Id:'20230129', Name:'Emma' } )} //query傳參接受 this.Id = ths.$route.params.Id this.Name = this.$route.params.Name //路由 { path:'/second/:id/:name', name:'second', component:() => import('@/view/second') }
vue-router路由傳參,如果沒參數(shù)的話就跳轉(zhuǎn)回來
if (JSON.stringify(this.$route.params) == '{}') { ? ? this.$router.go(-1) }
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vite?Vue3?EsLint?Prettier配置步驟極簡方法詳解
這篇文章主要為大家介紹了Vite?Vue3?EsLint?Prettier配置步驟的極簡方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09解決vue-router在同一個路由下切換,取不到變化的路由參數(shù)問題
今天小編就為大家分享一篇解決vue-router在同一個路由下切換,取不到變化的路由參數(shù)問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09Vue子組件向父組件通信與父組件調(diào)用子組件中的方法
這篇文章主要介紹了Vue子組件向父組件通信與父組件調(diào)用子組件中的方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-06-06