Angular2 - changing location without triggering a Route

荒凉一梦 提交于 2019-12-12 13:12:22

问题


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.

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?

I know that if I use:

location.go('users/3');

it will do above-mentioned things, but it also triggers Angular's route system, which in turn calls my services again.

Edit:

it seems like location's function

location.replaceState('users/3');

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.


回答1:


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]).

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.



来源:https://stackoverflow.com/questions/34876663/angular2-changing-location-without-triggering-a-route

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!