除了使用 <router-link> 创建 a 标签来定义导航链接,我们还可以借助 router 的实例方法,通过编写代码来实现。当你点击 <router-link> 时,这个方法会在内部调用,所以说,点击 <router-link :to="..."> 等同于调用 router.push(...)

声明式 编程式
<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' }}) const userId = 123
router.push({ name: 'user', params: { userId }}) // -> /user/123
router.push({ path: `/user/${userId}` }) // -> /user/123
// 这里的 params 不生效
router.push({ path: '/user', params: { userId }}) // -> /user

router.replace(location, onComplete?, onAbort?)

跟 router.push 很像,唯一的不同就是,它不会向 history 添加新记录,而是跟它的方法名一样 —— 替换掉当前的 history 记录。

声明式 编程式
<router-link :to="..." replace> router.replace(...)

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)

router.go,router.push,router.replace的区别的更多相关文章

  1. VUE的两种跳转push和replace对比区别

    router.push(location) 在vue.js中想要跳转到不同的 URL,需要使用 router.push 方法. 这个方法会向 history 栈添加一个新的记录,当用户点击浏览器后退按 ...

  2. [Vue]vue-router的push和replace的区别

    1.this.$router.push() 描述:跳转到不同的url,但这个方法会向history栈添加一个记录,点击后退会返回到上一个页面. 2.this.$router.replace() 描述: ...

  3. react中路由跳转push与replace的区别

    路由跳转,replace / push 区别 push: a-b-c 可以回到上一级 例: this.props.history.push('路由地址') replace: a-b-c 回不到上一级 ...

  4. React native路由跳转navigate、push、replace的区别

    由于没有系统的去学习RN,对路由跳转了解不多,只是跟着项目在做,抽点时间简单学习一下RN路由跳转方法区别,总结如下: 如上图,外部是一个栈容器,此时A页面在最底部,navigate到B页面,为什么此时 ...

  5. vue 中router.go;router.push和router.replace的区别

    vue 中router.go:router.push和router.replace的区别:https://blog.csdn.net/div_ma/article/details/79467165 t ...

  6. [Angular2 Router] Programmatic Router Navigation via the Router API - Relative And Absolute Router Navigation

    In this tutorial we are going to learn how to navigate programmatically (or imperatively) by using t ...

  7. window.location.href 和 window.location.replace 的区别

    window.location.href  和  window.location.replace 的区别 1.window.location.href=“url”:改变url地址: 2.window. ...

  8. Document.location.href和.replace的区别

    转自:https://www.cnblogs.com/GT_Andy/archive/2007/10/31/1922138.html 1 Document.location.href和.replace ...

  9. push和commit的区别

    push和commit的区别 git作为支持分布式版本管理的工具,它管理的库(repository)分为本地库.远程库.git commit操作的是本地库,git push操作的是远程库. git c ...

  10. push和pop的区别?

    1.push是什么?(推进) push就是推,延伸为推进.这个它是汇编的一个指令,(在其它语言中也可能会见到它).意思都是差不多的,就是把一个元素放入栈中.你可以假想栈是一个放光盘的那种盒子,有底没盖 ...

随机推荐

  1. 各大知名区块链交易所链接及API文档链接

    区块链交易所链接 火币网(Huobi):https://www.huobi.br.com/zh-cn/ API文档:https://github.com/huobiapi/API_Docs/wiki ...

  2. mysql在linux上的安装

    前提: 环境:workstation 11 + CentOS 7 + mysql-5.6.40 安装前先查看服务器里是否有老版本的mysql已经被安装了 rpm -qa|grep mysql 如果有就 ...

  3. 014-并发编程-java.util.concurrent之-CountDownLatch

    一.概述 CountDownLatch是JAVA提供在java.util.concurrent包下的一个辅助类,指定的一个或多个线程等待其他线程执行完成后执行. 能够使一个线程等待其他线程完成各自的工 ...

  4. DataTable 指定位置添加列

    dt.Columns.Add("id").SetOrdinal(指定位置);

  5. 图片在IE8浏览器多一个有边框问题解决办法

    最后在网上找了一下答案,IE8浏览器图片多一个有色边框,而Chrome浏览器没有边框的解决办法.   指定img的边框样式:   img{border-style:none;}

  6. 如何在CentOS 7中安装最新Git(源码安装)

    如何在CentOS 7中安装最新Git 2017年05月20日 11:49:53 阅读数:1624 Git是在今天的软件开发行业一个非常有用的版本控制工具.我一直使用Git.于是为Linux公社的读者 ...

  7. dfs模版

    dfs #include <stdio.h> #include <string.h> char Map[16][16]; int mv[16][16]; //mv[i][j] ...

  8. extjs model store学习笔记

    http://docs.sencha.com/extjs/6.2.0/guides/core_concepts/data_package.html // 定义一个ModelExt.define('My ...

  9. 用iframe嵌入了一个微信公众号平台文章的URL

    JS: $.ajaxPrefilter( function (options) { if (options.crossDomain && jQuery.support.cors) { ...

  10. [LeetCode] 240. Search a 2D Matrix II_Medium tag: Binary Search

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...