简体   繁体   English

Vue路由器上一页,无需重新加载

[英]Vue Router previous page without reload

I'm trying to create a back button to go to the previous page using the vue-router in vuejs that would work exactly like the browser's back button. 我正在尝试使用vuejs中的vue-router创建一个后退按钮以转到上一页,它的工作原理与浏览器的后退按钮完全一样。 The problem is, when using history mode for the router, when I do 问题是,当路由器使用历史记录模式时,

this.$router.go(-1);

it actually refreshes the page, which is quite annoying, and the browser's back button does not. 它实际上会刷新页面,这很烦人,而浏览器的“后退”按钮却没有。 If I remove history mode, it works as expected, exactly like the browser's back button. 如果删除历史记录模式,则它会按预期工作,就像浏览器的“后退”按钮一样。 However, in this case, I need to deal with the ugly hashtag in the URL. 但是,在这种情况下,我需要处理URL中的丑陋标签。 Also, since I only need this button for a few pages, I could also use keep history mode, but instead of making the back button actually go to the previous page, I could toggle around some values to change which dynamic component is shown, but not actually change the current page. 另外,由于我只需要在几个页面上使用此按钮,所以我也可以使用“保留历史记录”模式,但不必使后退按钮实际转到上一页,而是可以切换一些值来更改显示哪个动态组件,但是实际不更改当前页面。 However, if I do this, the browser's back button wouldn't work as expected anymore, since it clearly can't toggle the state. 但是,如果执行此操作,浏览器的后退按钮将无法再按预期工作,因为它显然无法切换状态。

Is there any way to make a button that works exactly as the browser's back button does? 有什么方法可以使按钮的功能与浏览器的后退按钮完全相同? As in, make a back button that does not refresh the page, not have a hashtag in the URL, and still work with the browser back buttons? 像这样,创建一个后退按钮,它不会刷新页面,在URL中没有井号标记,并且仍然可以与浏览器的后退按钮一起使用吗?

EDIT: Was going to delete since I realised it's not a problem with the code, it's a problem with the testing, but I'll keep it here just in case it's useful to someone. 编辑:将要删除,因为我意识到这不是代码的问题,这是测试的问题,但是我会保留在这里,以防万一它对某人有用。 When I was going to my route with the back button, there was no button to go to that route, so I would need to directly enter it into the URL. 当我使用后退按钮转到路线时,没有按钮可以转到该路线,因此我需要直接将其输入URL。 However, this effectively wipes all the stored states, so my back button didn't work properly. 但是,这有效地擦除了所有存储的状态,因此我的后退按钮无法正常工作。 If I navigated to that page from another page, then my back button worked as expected. 如果我从另一个页面导航到该页面,那么我的后退按钮将按预期工作。

您可以使用window.history.back()直接模拟点击浏览器的后退按钮。

这样尝试

 this.$router.back() 

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

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