Skip to content

Instantly share code, notes, and snippets.

@eliotsykes
Last active August 15, 2024 15:15
Show Gist options
  • Save eliotsykes/8954cf64fcd0df16f519 to your computer and use it in GitHub Desktop.
Save eliotsykes/8954cf64fcd0df16f519 to your computer and use it in GitHub Desktop.
How to get the current route, queryParams, etc. in an Ember component
// Examples
// Yes, "router.router" twice - this assumes that the router is being injected
// into the component. Otherwise lookup 'router:main'
// One of these will be of interest, figure out which one you want:
this.get('router.router.state');
this.get('router.router.state.params');
this.get('container').lookup('controller:application').currentPath;
// May work:
this.get('container').lookup('router:main').router.state;
@CharlesKozel
Copy link

Since Ember 2.15 you can do this through the public Router service.

router: service(),
myRouteName: computed('router.currentRouteName', function () {
    return this.get('router.currentRouteName') + 'some modification';
}

https://www.emberjs.com/api/ember/release/classes/RouterService

Which worked really well for me since I wanted something computed off of the current route. The service exposes currentRouteName, currentURL, location, and rootURL.

currentURL has the query params, but you would need to parse them from the URL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment