Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

18
  • 97
    The Location should be imported from "angular2/router" in older releases of Angular 2. In newer releases, it should be from "@angular/common". Commented May 12, 2016 at 14:37
  • 3
    If you have it built in in the framework, I dont see any reason to use the "native" "window.history.back();" which is an HTML5 feature (developer.mozilla.org/en-US/docs/Web/API/Window/history) Commented Jul 21, 2016 at 13:55
  • 10
    For what it's worth, the official Angular2 API documentation for Location states: "Note: it's better to use Router service to trigger route changes. Use Location only if you need to interact with or create normalized URLs outside of routing." @Sasxa's answer does apparently show a way to use Router to do this. However, the Location method definitely is more convenient. Does anyone know why the Router method might be more correct than the Location method? Commented Sep 16, 2016 at 14:50
  • 2
    @Andrew: I've encountered the problem, that you cannot go back two times, if you use this.location.back(). You will jump back to the initial site. Commented Sep 19, 2016 at 11:15
  • 1
    @yt61, not sure, maybe re-usability? or if you can get to a specified page from various routes, so you dont know in advance the route to go back to. Commented May 16, 2017 at 7:07