vue-router:使用浏览器后退按钮时跳过页面 [英] vue-router: skip page when using browser back button

查看:71
本文介绍了vue-router:使用浏览器后退按钮时跳过页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的申请中有一个主页"页面和一个成功"页面.

I have a "Home" page and a "Success" page in my application.

成功页面有一个按钮,点击该按钮后,会通过 window.location.href='https://google> 转到类似于 https://google.com 的 URL.com'.

The Success page has a button that, when clicked, goes to a URL like https://google.com, via window.location.href='https://google.com'.

我可以从首页开始,使用vue-router推送到成功页面,然后点击按钮跳转到网址.但是,当我按下浏览器的后退按钮时,我希望它返回主页而不是成功页面.

I can start on the Home page, use vue-router to push to the Success page, and then click the button to go to the URL. However, when I press the browser's back button, I'd like it to go back to the Home page instead of the Success page.

我该怎么做?

推荐答案

假设您的 javascript 在 Vue 实例中,您可以在设置 window 之前添加对 $router.replace 的调用.location.href.

Assuming your javascript is within a Vue instance, you can add a call to $router.replace right before setting window.location.href.

它看起来像这样:

methods: {
  onLinkClick() {
    this.$router.replace({ name: 'home' }); // or whatever your home page name is
    window.location.href='https://google.com';
  }
}

这是 vue-router 程序化导航的文档.

这篇关于vue-router:使用浏览器后退按钮时跳过页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆