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;
@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