question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ability to check for previous route with router.events.subscribe

See original GitHub issue

I’m submitting a … (check one with “x”)

[ ] bug report => search github for a similar issue or PR before submitting
[X] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

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:

  • Angular version: 2.0.0-rc.5
  • Browser: [all ]
  • Language: [all]

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:31 (5 by maintainers)

github_iconTop GitHub Comments

24reactions
jjlorenzocommented, Jan 10, 2017

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!

22reactions
DzmitryShylovichcommented, Jan 10, 2017
import { RoutesRecognized} from '@angular/router';
.filter(e => e instanceof RoutesRecognized)
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to determine previous page URL in Angular?
You can subscribe to route changes and store the current event so you can use it when the next happens previousUrl: string; constructor(router:...
Read more >
How to access the previous route in your Angular app
First create a new service PreviousRouteService . If you're using the Angular CLI , use the command ng generate service previous-route and add ......
Read more >
Subscribing To Router Events In Angular - Upmostly
It's pretty common in an Angular project to add the ability to “listen” for router events. That is, know that a user is...
Read more >
Accessing the Previous URL in Angular | by Jacob Neterer
Then, you will subscribe to router events and filter those events by the NavigationEnd type event. This gives us access to the current...
Read more >
Using Router Events To Detect Back And Forward Browser ...
To explore this Router behavior, I created a simple demo in which you can navigate between three routes, each of which has three...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found