vue router.push(),router.replace(),router.go()


1.router.push(location)=====window.history.pushState

想要導航到不同的 URL,則使用 router.push 方法。這個方法會向 history 棧添加一個新的記錄,所以,當用戶點擊瀏覽器后退按鈕時,則回到之前的 URL。

1
2
3
4
5
6
7
8
9
10
11
// 字符串
router.push( 'home' )
 
// 對象
router.push({ path:  'home'  })
 
// 命名的路由
router.push({ name:  'user' params : { userId: 123 }})
 
// 帶查詢參數,變成 /register?plan=private
router.push({ path:  'register' , query: { plan:  'private'  }})

 

2.router.replace(location)=====window.history.replaceState

跟 router.push 很像,唯一的不同就是,它不會向 history 添加新記錄,而是跟它的方法名一樣 —— 替換掉當前的 history 記錄

 

3.router.go(n)====window.history.go

1
2
3
4
5
6
7
8
9
10
11
12
// 在瀏覽器記錄中前進一步,等同於 history.forward()
router.go(1)
 
// 后退一步記錄,等同於 history.back()
router.go(-1)
 
// 前進 3 步記錄
router.go(3)
 
// 如果 history 記錄不夠用,那就默默地失敗唄
router.go(-100)
router.go(100)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM