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

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

想要导航到不一样的 URL,则使用 router.push 方法。这个方法会向 history 栈添加一个新的记录,因此,当用户点击浏览器后退按钮时,则回到以前的 URL。浏览器

// 字符串
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 记录code

 

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

// 在浏览器记录中前进一步,等同于 history.forward()
router.go(1)

// 后退一步记录,等同于 history.back()
router.go(-1)

// 前进 3 步记录
router.go(3)

// 若是 history 记录不够用,那就默默地失败呗
router.go(-100)
router.go(100)
相关文章
相关标签/搜索