vue路由跳轉的帶參與不帶參,路由跳轉傳參方式:name 、 path

前言:

       在vue項目中路由的router跳轉是很重要的,這裏有我自用跳轉的方法,最底下還有大神的方法(很詳細了);

自己目前用到的方法一:用path跳轉加參數,相當於get請求,頁面可以看到你傳參

第一:設置路由

{path: 'Train/studyDetails/:detailId', component: () => import('./views/team/Train/studyDetails'), meta: { title: '培訓學習詳情' }},

第二:父組件傳值

 routerDeatils(data){//點擊表格跳路由
            let nowId = '7';
            this.$router.push({path:'/team/Train/studyDetails/'+nowId});
        },

第三:在子組件裏面接收這個參數

created(){
      this.detailId = this.$route.params.detailId;
    },

自己目前用到的方法二:用name跳轉,params傳參,相當於post請求,頁面不可以看到你傳參值

第一:在跳轉頁面用name來定義跳轉的路徑,用params來定義需要攜帶的參數

  /**
     * 跳轉到詳情頁面
     */
    sendDetails(id){
      this.$router.push({ name:'readDetails', params:{ id : id  } });//用name跳轉的方式可以實現帶參數,而且不在頁面顯示,注意用path跳轉的話,params是不生效的
    }

第二:在調用頁面獲取id

let id  = this.$route.params.id;

------------------------------------------------------------------------------------------------------------------------------------

更多方法

1.  router-link

1. 不帶參數
 
<router-link :to="{name:'home'}"> 
<router-link :to="{path:'/home'}"> //name,path都行, 建議用name  
// 注意:router-link中鏈接如果是'/'開始就是從根路由開始,如果開始不帶'/',則從當前路由開始。
 
 
 
2.帶參數
 
<router-link :to="{name:'home', params: {id:1}}">  
 
// params傳參數 (類似post)
// 路由配置 path: "/home/:id" 或者 path: "/home:id" 
// 不配置path ,第一次可請求,刷新頁面id會消失
// 配置path,刷新頁面id會保留
 
// html 取參  $route.params.id
// script 取參  this.$route.params.id
 
 
<router-link :to="{name:'home', query: {id:1}}"> 
 
// query傳參數 (類似get,url後面會顯示參數)
// 路由可不配置
 
// html 取參  $route.query.id
// script 取參  this.$route.query.id

2.  this.$router.push() (函數裏面調用)

1.  不帶參數
 
this.$router.push('/home')
this.$router.push({name:'home'})
this.$router.push({path:'/home'})
 
 
 
2. query傳參 
 
this.$router.push({name:'home',query: {id:'1'}})
this.$router.push({path:'/home',query: {id:'1'}})
 
// html 取參  $route.query.id
// script 取參  this.$route.query.id
 
 
 
3. params傳參
 
this.$router.push({name:'home',params: {id:'1'}})  // 只能用 name
 
// 路由配置 path: "/home/:id" 或者 path: "/home:id" ,
// 不配置path ,第一次可請求,刷新頁面id會消失
// 配置path,刷新頁面id會保留
 
// html 取參  $route.params.id
// script 取參  this.$route.params.id
 
 
 
4. query和params區別
query類似 get, 跳轉之後頁面 url後面會拼接參數,類似?id=1, 非重要性的可以這樣傳, 密碼之類還是用params刷新頁面id還在
 
params類似 post, 跳轉之後頁面 url後面不會拼接參數 , 但是刷新頁面id 會消失

3.  this.$router.replace() (用法同上,push)

4.  this.$router.go(n) 

this.$router.go(n)
向前或者向後跳轉n個頁面,n可爲正整數或負整數
ps : 區別

this.$router.push
跳轉到指定url路徑,並想history棧中添加一個記錄,點擊後退會返回到上一個頁面
this.$router.replace
跳轉到指定url路徑,但是history棧中不會有記錄,點擊返回會跳轉到上上個頁面 (就是直接替換了當前頁面)

this.$router.go(n)
向前或者向後跳轉n個頁面,n可爲正整數或負整數

參考文獻:

1.https://blog.csdn.net/jiandan1127/article/details/86170336 

2.https://blog.csdn.net/weixin_30443895/article/details/95238305

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章