Angular2-在不触发路线的情况下更改位置 [英] Angular2 - changing location without triggering a Route
问题描述
我有一个使用服务加载项目列表的视图.我始终只显示一个项目,在各个项目之间以<<< >>按钮.
I have a view which uses service to load a list of items. I'm always displaying only one item, moving between items with << >> buttons.
是否可以在不触发Angular路由系统的情况下更改浏览器地址栏中的地址并将记录添加到浏览器的历史记录中?
Is there any way to change address in browser's address bar and to add record to browser's history without triggering Angular's routing system?
我知道如果我使用:
location.go('users/3');
它将完成上述操作,但也会触发Angular的路由系统,该路由系统又会再次调用我的服务.
it will do above-mentioned things, but it also triggers Angular's route system, which in turn calls my services again.
似乎是位置的功能
location.replaceState('users/3');
接近我想做的事-它在浏览器中更改url而不触发任何Angular内部结构,不幸的是,它取代了历史记录中的最新位置,而不是将新位置推到顶部.
is getting close to what I want to do - it changes url in browser without triggering any Angular internals, unfortunately it replaces latest location in history instead of pushing the new one at the top.
推荐答案
如果不将服务添加到组件provider:
列表中,则不会获得该服务的新实例.只需在bootstrap(AppComponent, [MyGlobalService])
中注册,即可将服务设为全局.
If you don't add the service to the components provider:
list you won't get a new instance for this service. Make the service global instead by only registering in bootstrap(AppComponent, [MyGlobalService])
.
实施 CanReuse 另请参见 Angular 2:在不同组件之间交换而不破坏它们可能有助于防止在重复使用同一路由时重新初始化组件.
Implementing CanReuse see also Angular 2: Swapping between different components without destroying them might help to prevent re-initializing components when the same route is reused.
这篇关于Angular2-在不触发路线的情况下更改位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!