Skip to content

Instantly share code, notes, and snippets.

@eliotsykes
Last active November 13, 2019 13:06
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • 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;
@timmyomahony
Copy link

timmyomahony commented Feb 12, 2016

I'm new to Ember so I could be wrong, but I think using this.get('container') is now depreciated. Instead this should work:

import Ember from 'ember';
const {
  getOwner
} = Ember;
...
getOwner(this).lookup('controller:application').currentPath

@ghoshnirmalya
Copy link

@jilucev
Copy link

jilucev commented Oct 18, 2017

🌮 Thank you!

@aaronlelevier
Copy link

aaronlelevier commented Aug 3, 2018

The query param can be passed to the component in the template.hbs for the component. This worked for me.

I found it in this StackOverflow post: https://stackoverflow.com/questions/30948579/passing-query-parameters-into-component-emberjs

@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