關於編程式導航$router.push、replace、go的用法區別

  1. 如果是想要跳轉到不同的url,可以使用router.push方法,這個方法會向history裏面添加記錄,是可以通過瀏覽器的返回按鈕,返回到之前的頁面的。
// 字符串
router.push('home')
// 對象
router.push({ path: 'home' })
// 命名的路由帶參數
router.push({ name: 'user', params: { userId: '123' }})
// 獲取跳轉後的頁面參數
this.userId = this.$route.params.userId

// 帶查詢參數,變成 /register?plan=private
router.push({ path: 'register', query: { plan: 'private' }})
// 獲取跳轉後的頁面參數
this.plan = this.$route.query.plan

  1. router.replace和 router.push 很像,但是他是不會向history裏面田間新的記錄的。點擊返回,會跳轉到上上一個頁面。上一個記錄是不存在的。

  2. router.go(n)這個方法的參數是一個整數,意思是在 history 記錄中向前或者後退多少步,類似 window.history.go(n)。

// 在瀏覽器記錄中前進一步,等同於 history.forward()
router.go(1)

// 後退一步記錄,等同於 history.back()
router.go(-1)

// 前進 3 步記錄
router.go(3)

// 如果 history 記錄不夠用,那就默默地失敗唄
router.go(-100)
router.go(100)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章