我们在 vue-router 中有 router.reload 吗? [英] Do we have router.reload in vue-router?
问题描述
我在这个拉取请求中看到:
添加一个
router.reload()
重新加载当前路径并再次调用数据挂钩
Reload with current path and call data hook again
但是当我尝试从 Vue 组件发出以下命令时:
But when I try issuing the following command from a Vue component:
this.$router.reload()
我收到此错误:
未捕获的类型错误:this.$router.reload 不是函数
我在 docs 中进行了搜索,但可以没有找到任何相关的东西.vue/vue-router 是否提供了一些这样的功能?
I searched in the docs, but could not found anything relevant. Does vue/vue-router provider some functionality like this?
我感兴趣的软件版本是:
The software versions I'm interested in are:
"vue": "^2.1.0",
"vue-router": "^2.0.3",
附注.我知道 location.reload()
是其中一种选择,但我正在寻找本机 Vue 选项.
PS. I know location.reload()
is one of the alternatives, but I'm looking for a native Vue option.
推荐答案
this.$router.go()
正是这样做的;如果没有指定参数,路由器将导航到当前位置,刷新页面.
this.$router.go()
does exactly this; if no arguments are specified, the router navigates to current location, refreshing the page.
注意:当前路由器的实现而它的历史组件没有将参数标记为可选,但 IMVHO 是 Evan You 的错误或遗漏,因为 规范明确允许.我已经提交了一份关于它的问题报告.如果你真的关心当前 TS 注释,只需使用等效的 this.$router.go(0)
note: current implementation of router and its history components don't mark the param as optional, but IMVHO it's either a bug or an omission on Evan You's part, since the spec explicitly allows it. I've filed an issue report about it. If you're really concerned with current TS annotations, just use the equivalent
this.$router.go(0)
至于'为什么会这样':go
在内部将其参数传递给 window.history.go
,因此它等于 windows.history.go()
- 反过来,根据 MDN 文档.
As to 'why is it so': go
internally passes its arguments to window.history.go
, so its equal to windows.history.go()
- which, in turn, reloads the page, as per MDN doc.
注意:因为这会执行一个软"在常规桌面(非便携式)Firefox 上重新加载,如果您使用它可能会出现一堆奇怪的怪癖,但实际上您需要真正的重新加载;使用 window.location.reload(true);
(https://developer.mozilla.org/en-US/docs/Web/API/Location/reload)可能会有所帮助 - 它确实解决了我在 FF 上的问题.>
note: since this executes a "soft" reload on regular desktop (non-portable) Firefox, a bunch of strange quirks may appear if you use it but in fact you require a true reload; using the
window.location.reload(true);
(https://developer.mozilla.org/en-US/docs/Web/API/Location/reload) mentioned by OP instead may help - it certainly did solve my problems on FF.
这篇关于我们在 vue-router 中有 router.reload 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!