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.
isActive (routeName, models, options) Boolean public
Defined in packages/@ember/-internals/routing/lib/services/router.ts:129
- routeName
- String
- the name of the route
- models
- ...Object
- the model(s) or identifier(s) to be used while transitioning to the route.
- options
- Object
- optional hash with a queryParams property containing a mapping of query parameters
- returns
- Boolean
- true if the provided routeName/models/queryParams are active
Determines whether a route is active.
recognize (url) public
Defined in packages/@ember/-internals/routing/lib/services/router.ts:309
- url
- String
Takes a string URL and returns a RouteInfo
for the leafmost route represented
by the URL. Returns null
if the URL is not recognized. This method expects to
receive the actual URL as seen by the browser including the app's rootURL
.
recognizeAndLoad (url) public
Defined in packages/@ember/-internals/routing/lib/services/router.ts:328
- url
- String
Takes a string URL and returns a promise that resolves to a
RouteInfoWithAttributes
for the leafmost route represented by the URL.
The promise rejects if the URL is not recognized or an unhandled exception
is encountered. This method expects to receive the actual URL as seen by
the browser including the app's rootURL
.
replaceWith (routeNameOrUrl, models, options) Transition public
Defined in packages/@ember/-internals/routing/lib/services/router.ts:89
- routeNameOrUrl
- String
- the name of the route or a URL
- models
- ...Object
- the model(s) or identifier(s) to be used while transitioning to the route.
- options
- Object
- optional hash with a queryParams property containing a mapping of query parameters
- returns
- Transition
- the transition object associated with this attempted transition
Transition into another route while replacing the current URL, if possible. The route may be either a single route or route path:
See replaceWith for more info.
Calling replaceWith
from the Router service will cause default query parameter values to be included in the URL.
This behavior is different from calling replaceWith
on a route.
See the Router Service RFC for more info.
transitionTo (routeNameOrUrl, models, options) Transition public
Defined in packages/@ember/-internals/routing/lib/services/router.ts:56
- routeNameOrUrl
- String
- the name of the route or a URL
- models
- ...Object
- the model(s) or identifier(s) to be used while transitioning to the route.
- options
- Object
- optional hash with a queryParams property containing a mapping of query parameters
- returns
- Transition
- the transition object associated with this attempted transition
Transition the application into another route. The route may be either a single route or route path:
See transitionTo for more info.
Calling transitionTo
from the Router service will cause default query parameter values to be included in the URL.
This behavior is different from calling transitionTo
on a route or transitionToRoute
on a controller.
See the Router Service RFC for more info.
urlFor (routeName, models, options) String public
Defined in packages/@ember/-internals/routing/lib/services/router.ts:113
- routeName
- String
- the name of the route
- models
- ...Object
- the model(s) or identifier(s) to be used while transitioning to the route.
- options
- Object
- optional hash with a queryParams property containing a mapping of query parameters
- returns
- String
- the string representing the generated URL
Generate a URL based on the supplied route name.