Last active
August 15, 2024 15:15
-
-
Save eliotsykes/8954cf64fcd0df16f519 to your computer and use it in GitHub Desktop.
How to get the current route, queryParams, etc. in an Ember component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
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
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