问题
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