Class RouterService
publicThe Router service is the public API that provides access to the router.
The immediate benefit of the Router service is that you can inject it into components, giving them a friendly way to initiate transitions and ask questions about the current global router state.
In this example, the Router service is injected into a component to initiate a transition to a dedicated route:
1 2 3 4 5 6 7 8 9 10 11 12 |
import Component from '@ember/component'; import { inject as service } from '@ember/service'; export default Component.extend({ router: service(), actions: { next() { this.get('router').transitionTo('other.route'); } } }); |
Like any service, it can also be injected into helpers, routes, etc.
currentRoute public
Defined in packages/@ember/-internals/routing/lib/services/router.ts:296
A RouteInfo
that represents the current leaf route.
It is guaranteed to change whenever a route transition
happens (even when that transition only changes parameters
and doesn't change the active route)
currentRouteName public
Defined in packages/@ember/-internals/routing/lib/services/router.ts:165
Name of the current route.
This property represents the logical name of the route, which is comma separated. For the following router:
app/router.js | |
1 2 3 4 5 6 |
Router.map(function() { this.route('about'); this.route('blog', function () { this.route('post', { path: ':post_id' }); }); }); |
It will return:
index
when you visit/
about
when you visit/about
blog.index
when you visit/blog
blog.post
when you visit/blog/some-post-id
currentURL public
Defined in packages/@ember/-internals/routing/lib/services/router.ts:194
Current URL for the application.
1 2 |
This property represents the URL path for this route. For the following router: |
app/router.js | |
1 2 3 4 5 6 |
Router.map(function() { this.route('about'); this.route('blog', function () { this.route('post', { path: ':post_id' }); }); }); |
It will return:
/
when you visit/
/about
when you visit/about
/blog
when you visit/blog
/blog/some-post-id
when you visit/blog/some-post-id
location public
Defined in packages/@ember/-internals/routing/lib/services/router.ts:222
The location
property determines the type of URL's that your
application will use.
The following location types are currently available:
auto
hash
history
none
rootURL public
Defined in packages/@ember/-internals/routing/lib/services/router.ts:238
The rootURL
property represents the URL of the root of
the application, '/' by default.
This prefix is assumed on all routes defined on this app.
IF you change the rootURL
in your environment configuration
like so:
config/environment.js | |
1 2 3 4 5 6 7 8 9 10 |
module.exports = function(environment) { let ENV = { modulePrefix: 'router-service', environment, rootURL: '/my-root', … } ]; |
This property will return /my-root
.