-
Notifications
You must be signed in to change notification settings - Fork 25.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ability to check for previous route with router.events.subscribe #11268
Comments
You can use pairwise method to get previous and the current value |
Thank you this worked perfectly. Code for reference.
|
I'm having problem with this solution. The code inside the subscribe does not run when I enter the component for the very first time, that is when I need to get the previous url. |
Is this code in your main app component? It shouldn't be in a child component. |
tl;dr:
this.router.events.bufferCount(6).subscribe((e: any[]) => {
console.log('previous', e[1].urlAfterRedirects);
}); e = [
NavigationStart, RoutesRecognized, NavigationEnd // previous route info
NavigationStart, RoutesRecognized, NavigationEnd // current route info
]
|
If you only use |
After following your recommendation. This is my working code. import { Component } from '@angular/core';
import { OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { LocalStorageService } from 'angular-2-local-storage';
@Component({
selector: 'app-root',
template: `<router-outlet></router-outlet>`,
})
export class AppComponent implements OnInit {
constructor(
private router: Router,
private storage: LocalStorageService,
) { }
ngOnInit() {
this.router.events
.filter(e => e.constructor.name === 'RoutesRecognized')
.pairwise()
.subscribe((e: any[]) => {
this.storage.set('referrer', e[0].urlAfterRedirects);
});
}
} Thank you very much! |
|
Thanks!, fixed in my code. |
Isn't it sad that we need the localstorage to fetch the last route in a component? I guess this should be a pretty simple use case. |
This was my first approach, using the local storage, then I realize that persisting this value wasn't necessary, so I convert into a service |
I just wanted to store previous route, this worked for me although I'm sure I'm sure I'm missing something from above.
|
@helzgate You should be filtering your Router events to look for the event that has the property you seek, urlAfterRedirects, which is RoutesRecognized. This can be done with:
On topic: Just highlighting the fact that this is far from an ideal solution. What you're suggesting here is keeping track of the second to last route navigated to, which is not the same as the second to last route in navigation history. Use navigateByUrl with replaceUrl: true and your history is no longer accurate. @vsavkin Could you please point us towards a conclusive solution? Is there a way to do this in a consistent manner? |
Am I missing something really obvious here? import { Component, OnInit } from '@angular/core';
import { Router, RoutesRecognized } from '@angular/router';
import { LocalStorageService } from 'angular-2-local-storage';
import 'rxjs/add/operator/pairwise';
@Component({
selector: 'app',
template: `
<Content>
<router-outlet></router-outlet>
</Content>
`,
})
export class AppComponent implements OnInit {
constructor(
private router: Router,
private storage: LocalStorageService,
) { }
ngOnInit() {
this.router.events
.filter((e: any) => e instanceof RoutesRecognized)
.pairwise()
.subscribe((e: any) => {
console.log(e);
});
}
} Gives me:
|
|
I needed to implement a return link and it sent me down the RxJs rabbit hole. The implementation with the pairwise will not work if the user doesn't trigger navigation event at least two times and may cause a small bug. page 1 -> page 2
page 2 -> page 3
A better implementation could potentially use bufferCount (RxJs 5) if NavigationStart triggered on the page that links to the other page (page 1 and then page 2)? However, it seems that NavigationStart doesn't trigger on the page that links to the other page, but to the destination page (page 2). Is that the expected behaviour? If so, then what is the difference between NavigationStart and NavigationEnd?
Pairwise documentation:
which is because of this if check in the code:
The pairwise can be hacked to return [undefined, element] event if there is only 1 element in the buffer easily
Maybe I should take this over to RxJs team? |
@HoumanKml
I think you need no Rx, just this service: https://angular.io/docs/ts/latest/api/common/index/Location-class.html#!#back-anchor |
@tytskyi I admit there are other possible solutions to solve this and similar use cases and so adding a new feature or modifying the current behaviour of the navigationstart is not urgent. For example if one is using redux, there can be a location history array that is updated each time a navigation action is issued -- Though I don't like this particular approach because a global state would be redundant, as the navigation event observable is exactly that global history array [and working with RxJs is more fun :D] |
Is there another issue that considers adding information about the previous route to the ActivatedRoute? This would be the most flexible and easy-to-use solution, since it can be taken advantage of e.g. in Guards. |
Hi all, this is exactly what i was looking for can someone help me understand how to use the RoutesRecognized properties so that I can track the routes and use that information to help the user navigate to the previous url?I tried logging the RouteRecognized but it is logging a function named RoutesRecognized with four properties in it Thanks. |
I am getting multiple events of the same type when subscribing to events upon changing the route: for example second time I change the route I will get 2 NavigationStart events, 2 NavigationEnd events etc. When I change the route for the 3rd time then there are 3 events of each type. I don't think that is expected - is there a problem in my app somewhere which fires those additional events? |
@deli6z , I used it like this and it solved my problem.
This will emit the first pair. If it's not a pair, it won't emit anything as per my observation. |
@DzmitryShylovich
I need to do pairwise but I need to return the first one even if there is no second value |
@HunderlineK you're the one true messiah and @DzmitryShylovich i dunno wtf is going on with all your suggestions being systematically unrunnable I'm going to assume it's just deprecation that made it this way in angular v.5. and it worked before, but these comments should be removed or edited there's people being thrown off course by them. |
Hi I have error in below code in Angular 5, Can anyone help
Error :
|
I want to get previous url on navigate new page
|
|
Can anybody tell me how to route from the same component in the next time? |
FYI for RxJS v6+ you need to use the router.events
.pipe(
filter(e => e instanceof NavigationEnd),
pairwise() // check it's second route load
)
.subscribe((e: any[]) => {
console.log(e);
}); |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
I'm submitting a ... (check one with "x")
Current behavior
The NavigationStart event object only returns the target url.
Expected/desired behavior
Have the event also return the previous url
What is the motivation / use case for changing the behavior?
I am having to call window.scrollTo(0,0) on every route call so that the new route doesn't load at a random point on the page. But I don't want this to happen on every route, and in some cases I want this to not happen when the routes are both children of the same component route.
Please tell us about your environment:
The text was updated successfully, but these errors were encountered: