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

1、router.push():导航到不同的url,向history栈添加一个新的记录。( === window.history.pushState)

声明式编程式 < router - link: to = "" > router.push()
// 字符串
router.push('home')

// 对象
router.push({
path: 'home'
})

// 命名的路由
router.push({
name: 'user',
params: {
userId: '123'
}
})

// 带查询参数,变成 /register?plan=private
router.push({
path: 'register',
query: {
plan: 'private'
}
}) 1 2 3 4 5 6 7 8 9 10 11 2、router.replace():导航到不同url,替换history栈中当前记录。( === window.history.replaceState)

声明式编程式 < router - link: to = ""replace > router.replace() 3、router.go(n):指定前进 / 回退的步数。n为正数的时候是前进;负数的时候是后退;0的时候是刷新当前页面。( === window.history.go)

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

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

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

// 如果 history 记录不够用,那就默默地失败呗
router.go( - 100) router.go(100) 1 2 3 4 5 6 7 8 9 10 11 12 4、router.forward():前进一步。5、router.back():回退一步。注意:当你点击 < router - link > 时,这个方法会在内部调用,所以说,点击 < router - link: to = "" > 等同于调用router.push()。

参考:Vue Route编程式的导航

原文链接:https: //blog.csdn.net/LonewoIf/article/details/90698486

原文地址:https://www.cnblogs.com/1549983239yifeng/p/14603039.html