天天看點

this.$router.push 通過代碼跳轉路由

在頁面中可以通過以下方式使用路由

1:通過router-link标簽的方式

<router-link to="home">Home</router-link>

2:通過router的push的方式

this.$router.push({path: ‘/manage’});

在執行點選按鈕跳轉頁面之前還會執行一系列方法,這時可以使用 this.$router.push(location) 來修改 url,完成跳轉。

push 後面可以是對象,也可以是字元串:

// 字元串
this.$router.push('/home/first')
// 對象
this.$router.push({ path: '/home/first' })
// 命名的路由
this.$router.push({ name: 'home', params: { userId: wise }})
           

通過代碼跳轉案例

<template>
  <div>
     <button @click='userClick'>使用者</button>
     <button @click='profileClick'>檔案</button>
  </div>
</template>
<script>
  export default{
    data(){
      return {
        userId:'wushen'
      }
    },
    methods:{
      userClick(){
        this.$route.push('/user/'+this.userId)
      },
      profileClick(){
        this.$route.push({
          path: '/profile',
          query: {
            name: 'wushen',
            age:18,
            height:1.88
          }
        })
      }
    }
  }
</script>
           

跳轉頁面并傳遞參數的方法:

1.Params

  • this.$router.push({name:"",params:{id:""}})

    name和params搭配重新整理參數會消失
  • this.$router.push({path:"",query:{id:""}})

    path和query搭配,重新整理頁面參數不會消失,query中參數成了url中的一部分

由于動态路由也是傳遞params的,是以在 this.$router.push() 方法中path不能和params一起使用,否則params将無效。需要用name來指定頁面。

及通過路由配置的name屬性通路

兩種方式的差別是query傳參的參數會帶在url後邊展示在位址欄,params傳參的參數不會展示到位址欄。

在路由配置檔案中定義參數:

/* router.js 檔案*/
import Vue from "vue";
import Router from "vue-router";
import MediaSecond from "@/views/EnterprisePage/MediaMatrix/second"; //資訊清單
 
Vue.use(Router);
export default new Router({
  routes: [ /* 進行路由配置 */
    {
        name: "MediaSecond",
        path: "/MediaSecond",
        component: MediaSecond
    },
  ]
})
 
/* 後面還需要接一空行,否則無法通過 ESlint 文法驗證 */
           

通過name擷取頁面,傳遞params:

this.$router.push({ name: 'MediaSecond',params:{artistName:artistName,imgUrl:imgUrl,type:2} })
           

在目标頁面通過this.$route.params擷取參數:

if (this.$route.params.type == 2) {
    this.type = apis.getAtistDetails;
} else {
    this.type = apis.getMessageList;
}
           

2.Query

頁面通過path/name和query傳遞參數,該執行個體中row為某行表格資料

this.$router.push({ name: 'DetailManagement', query: { auditID: row.id, type: '2' } });
this.$router.push({ path: '/DetailManagement', query: { auditID: row.id, type: '2' } });
           

在目标頁面通過this.$route.query擷取參數:

this.$route.query.type

1.需要注意的是接收參數的時候是route而不是router。兩種方式一一對應,名字不能混用。

2.由于動态路由也是傳遞params的,是以在 this.$router.push() 方法中path不能和params一起使用,否則params将無效。隻能用name來指定頁面。

3.不帶參數,直接跳轉頁面

this.$router.push('/orderList')
this.$router.push({name:'orderList'})
this.$router.push({path:'/orderList'})
           

用query傳參對象時需注意的地方

在vue項目中,我們用函數式程式設計this.$router.push跳轉,用query傳遞一個對象時要把這個對象先轉化為字元串,然後在接收的時候要轉化為對象,要不然會接收不到參數。要不就把參數分開傳遞,不要放到對象裡。

this.$router.push({
    path: '/liveing',
    query: {
        routeParams: JSON.stringify({ liveUrl: url, flag: 2 })
    }
});
           

接收:

let routeParams = JSON.parse(this.$route.query.routeParams)
this.currMeetingUrl = routeParams.liveUrl; 
this.obcject = routeParams.flag;
           

第二種方法:不要套在對象裡直接傳遞

this.$router.push({
    path: '/liveing',
    query: {
        liveUrl: url, 
        flag: 2
    }
});
           

接收:

let liveUrl = this.$route.query.liveUrl;
let flag = this.$route.query.flag;