在 Aurelia 中重新加载当前页面
我有一个 Aurelia 应用程序,用户可以在其中选择他们当前工作"的公司.应用程序中的每个页面都依赖于当前选择的公司,用户可以从下拉菜单中选择新公司.下拉菜单是位于导航栏上的一个组件.
I have an Aurelia application where the user can select the company they're currently "working on". Every page in the app is dependent on the currently selected company, and the user can select a new company from a drop-down menu. The drop-down is a component sitting on the nav-bar.
我想要的是让该组件在 change.delegate
处理程序上重新加载当前页面,而无需重新启动应用程序.所以设置 window.location.href
是不可能的.
What I'd like is to have that component reload the current page on the change.delegate
handler without restarting the app. So setting window.location.href
is out of the question.
有没有办法强制 aurelia Router
重新加载当前的路由/页面?
Is there a way to force the aurelia Router
to reload the current route/page?
另一种方法是使用 EventAggregator
来表示整个应用程序中的公司变更,但这需要在每个页面上订阅该事件或让每个页面从订阅的基类继承到那个事件,但这些是更多涉及的选项.
The alternative would be to use the EventAggregator
to signal a company change throughout the app, but that would require either subscribing to that event on every page or having every page inherit from a base class that subscribes to that event, but these are much more involved options.
这并没有回答您的确切问题,即如何强制重新加载当前视图,但是您是否考虑过添加当前视图?您路线的公司标识符?这将解决您的问题并使您的路线可加入书签:即:/#/company1/view1
和 /#/company2/view1
.当您在下拉列表中更改公司时,您可以使用新路线调用 router.navigate()
并且新视图将被加载/刷新.
This does not answer your exact question which is how to force a reload of the current view, but have you considered adding the current company identifier to your route(s)? This will solve your problem and make your routes bookmarkable: ie: /#/company1/view1
and /#/company2/view1
. When you change the company in the drop-down you call router.navigate()
with the new route and the new view will be loaded/refreshed.
为了回答您的确切问题,我测试了 router.navigate
选项:router.navigate('myroute', {replace:true, trigger:true})
和如果已经加载了正确的视图,它们都不会强制重新加载视图.通过将 ?ramdomNumber
附加到您的路线名称,您可以强制重新激活视图.
To answer your exact question, I tested the router.navigate
options: router.navigate('myroute', {replace:true, trigger:true})
and none of them force a reload of the view if the right view is already loaded. By appending ?ramdomNumber
to your route name you can force a reactivation of the view.