Vue中 this.$router.push() 實現頁面跳轉及傳參

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

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


// 字符串
this.$router.push('/viewAgent')
// 對象
this.$router.push({ path: '/viewAgent' })
// 命名的路由
this.$router.push({ name: 'viewAgent', params: { isShow: true}})

跳轉頁面並傳遞參數的方法:
1.Params 傳值 接收參數
由於動態路由也是傳遞params的,所以在 this.$router.push() 方法中path不能和params一起使用,否則params將無效。需要用name來指定頁面。及通過路由配置的name屬性訪問
在路由配置文件中定義參數:


/* 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
    },
  ]

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

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

在目標頁面通過this.$route.params獲取參數:

this.$route.params.type
this.$route.params.artistName
this.$route.params.imgUrl

2.query 傳值 接收參數
頁面通過path/name和query傳遞參數

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.auditID
this.$route.query.type

3. 通過跳轉打開新的標籤頁並定位到對應頁面

    let routeUrl = this.$router.resolve({
        path: '/viewAgent',   // 對應路由
        query: { isShow: false }   //傳值
      })
      window.open(routeUrl.href, '_blank')

接收參數

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