转载:https://www.cnblogs.com/lwwen/p/7245083.html

   https://blog.csdn.net/qq_15385627/article/details/83146766

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 记录

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)

附:----------------------------

vue.router中replace需要返回两次问题及解决方案

 
  • 问题:现有三个页面a , b , c , 递进关系,a页面router.push跳转至b,b再router.push跳转至c,c使用$router.replace()回到b,然后点击b页面的返回键,需要点击两次才能回到a页面
  • 需求:c保持router.replace至b方式不变,b页面返回键点击一次正常返回a页面
  • 解决方法:使用replace方法之后,再使用router.go(-1)方法返回一次就可以。
    例:
    this.router.replacePage({name:'b'})
    this.router.go(-1)
  • 原理:先解析一下几种页面跳转方式的不同,
  1. router.push : 跳转到新的页面,向history栈添加新一个新纪录,点击返回,返回到上一级页面。
  2. router.replace: 打开新的页面,不会像history添加新纪录,而是直接替换掉当前记录。
  3. router.go: 在history有记录的情况下,前进后退相应的页面。
    页面的跳转记录就是:a->b->c->b
    页面的堆栈记录则是:a->b->b
    那么返回的时候路线就是b->b->a,相当于b页面有两个,但由于b页面是一模一样的,所以视觉上是觉得点了两次返回键。
    那么解决的思路就很清晰了,减少history中的b的记录,就是在用b替换c页面记录同时,回退一页,记录就变成a->b,返回的时候自然是直接回到a。
 

vue router.push(),router.replace(),router.go()和router.replace后需要返回两次的问题的更多相关文章

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

    1.router.push(location)=====window.history.pushState 想要导航到不同的 URL,则使用 router.push 方法.这个方法会向 history ...

  2. router.go,router.push,router.replace的区别

    除了使用 <router-link> 创建 a 标签来定义导航链接,我们还可以借助 router 的实例方法,通过编写代码来实现.当你点击 <router-link> 时,这个 ...

  3. Vue页面跳转$router.push 的用法

    vue2.0在使用的过程中, .vue文件之间的跳转,在template中的常见写法是: <router-link to="/miniCard/statement/horizon&qu ...

  4. vue中this.$router.push()路由传值和获取的两种常见方法

    1.路由传值   this.$router.push() (1) 路由跳转使用router.push()方法,这个方法会向history栈添加一个新纪录,所以,当用户点击浏览器后退按钮时,会回到之前的 ...

  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. vue中router.go、router.push和router.replace的区别

    router.go(n) 这个方法的参数是一个整数,意思是在history记录中向前或者后退多少,类似window.history.go(n) router.push(location) 想要导航到不 ...

  7. vue 中router.go、router.push和router.replace的区别

    router.go(n) 这个方法的参数是一个整数,意思是在 history 记录中向前或者后退多少步,类似 window.history.go(n) router.push(location) 想要 ...

  8. Vue this.$router.push、replace、go的区别

    1.this.$router.push 描述:跳转到不同的url,但这个方法会向history添加一个记录,点击后会返回到上一个页面 用法 //字符串 this.$router.push('home' ...

  9. vue 之this.$router.push、replace、go的区别

    一.this.$router.push 说明:跳转到指定URL,向history栈添加一个新的记录,点击后退会返回至上一个页面 使用: this.$router.push('/index') this ...

随机推荐

  1. 7-10 多项式A除以B (25 分)

    题目链接:https://pintia.cn/problem-sets/1108548596745592832/problems/1108548661014913033 题目大意: 这仍然是一道关于A ...

  2. [转] 图 + 文 + 公式 理解LSTM

    转自公号“机器之心” LSTM入门必读:从入门基础到工作方式详解 长短期记忆(LSTM)是一种非常重要的神经网络技术,其在语音识别和自然语言处理等许多领域都得到了广泛的应用..在这篇文章中,Edwin ...

  3. pyinstaller 打包exe可执行文件

    Python打包EXE方法之一 一.安装Pyinstaller 1.安装pywin32 下载安装文件:查找到跟自己适用的python版本及window系统版本匹配的pywin32,下载后安装 使用pi ...

  4. inception_v2版本《Rethinking the Inception Architecture for Computer Vision》(转载)

    转载链接:https://www.jianshu.com/p/4e5b3e652639 Szegedy在2015年发表了论文Rethinking the Inception Architecture ...

  5. asyncio Queue的使用例子

    import aiohttp import asyncio import async_timeout from urllib.parse import urljoin, urldefrag root_ ...

  6. Flask请求流程超清大图

    补充一下 request是在哪里产生的: class RequestContext(object): # app就是flask对象 self.app = app if request is None: ...

  7. 题解-Codeforces710F String Set Queries

    咕了好久没更博客,最近得知可以去冬眠营玩耍,还可以搭顺风车回广州过年 (最近做到的比较有意思的题目:bzoj3958.hihocoder1419) Problem Codeforces-710F--洛 ...

  8. VS2015 代码片段整理

    1.什么是代码段? 将一段代码行提取出来,可以多次重复的使用.VS IDE提供对代码段的完整支持.使代码编写更快.更容易.更可靠. 2.系统默认代码段 对于开发人员的例行任务,Visual Studi ...

  9. makefile中的gcc -o $@ $^是什么意思?

    $@表示目标,$^表示依赖列表. 比如: edit : main.o kbd.o command.o display.o insert.o search.o files.o utils.o $@就是e ...

  10. vscode 的使用笔记

    1.使用vscode 的终端命令 ctrl + ~  打开 vs 的终端 这是使用windows 自带的shell终端, 使用git.bash的shell  在设置里面,找到 terminal.int ...