简体   繁体   English

vue-router:使用浏览器后退按钮时跳过页面

[英]vue-router: skip page when using browser back button

I have a "Home" page and a "Success" page in my application. 我的应用程序中有一个“主页”页面和一个“成功”页面。

The Success page has a button that, when clicked, goes to a URL like https://google.com , via window.location.href='https://google.com' . 成功页面上有一个按钮,单击该按钮时,会通过window.location.href='https://google.com'转到类似于https://google.com的URL。

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. 我可以从主页开始,使用vue-router推送到“成功”页面,然后单击按钮转到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. 但是,当我按下浏览器的“后退”按钮时,我希望它返回首页而不是“成功”页面。

How can I do this? 我怎样才能做到这一点?

Assuming your javascript is within a Vue instance, you can add a call to $router.replace right before setting window.location.href . 假设您的JavaScript在Vue实例中,则可以在设置window.location.href之前,立即向$router.replace添加调用。

It would look something like this: 它看起来像这样:

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

Here's the documentation for vue-router programmatic navigation . 这是vue-router程序化导航的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM