2015-04-28 06:22:25 -07:00
p.location-badge.
2015-06-01 22:51:00 -07:00
exported from <a href='../router'>angular2/router</a>
2015-06-19 12:08:22 -07:00
defined in <a href="https://github.com/angular/angular/tree/3a0410a/modules/angular2/src/router/router.ts#L9-L200">angular2/src/router/router.ts (line 9)</a>
2015-04-28 06:22:25 -07:00
:markdown
2015-05-18 17:39:04 -07:00
# Router
2015-04-28 06:22:25 -07:00
The router is responsible for mapping URLs to components.
You can see the state of the router by inspecting the read-only field `router.navigating`.
This may be useful for showing a spinner, for instance.
2015-06-01 22:51:00 -07:00
## Concepts
Routers and component instances have a 1:1 correspondence.
The router holds reference to a number of "outlets." An outlet is a placeholder that the
router dynamically fills in depending on the current URL.
2015-06-19 12:08:22 -07:00
When the router navigates from a URL, it must first recognizes it and serialize it into an
`Instruction`.
2015-06-01 22:51:00 -07:00
The router uses the `RouteRegistry` to get an `Instruction`.
2015-04-28 06:22:25 -07:00
.l-main-section
h2 Members
.l-sub-section
h3 constructor
pre.prettyprint
code.
2015-06-19 12:08:22 -07:00
constructor(_registry: RouteRegistry, _pipeline: Pipeline, parent: Router, hostComponent: any)
2015-04-28 06:22:25 -07:00
:markdown
2015-06-01 22:51:00 -07:00
2015-04-28 06:22:25 -07:00
.l-sub-section
2015-06-19 12:08:22 -07:00
h3 navigating
2015-04-28 06:22:25 -07:00
:markdown
2015-05-01 06:37:29 -07:00
2015-06-01 22:51:00 -07:00
2015-04-28 06:22:25 -07:00
.l-sub-section
2015-06-19 12:08:22 -07:00
h3 lastNavigationAttempt
2015-04-28 06:22:25 -07:00
:markdown
2015-05-01 06:37:29 -07:00
2015-06-01 22:51:00 -07:00
.l-sub-section
2015-06-19 12:08:22 -07:00
h3 previousUrl
2015-06-01 22:51:00 -07:00
:markdown
2015-04-28 06:22:25 -07:00
.l-sub-section
2015-06-19 12:08:22 -07:00
h3 parent
2015-04-28 06:22:25 -07:00
:markdown
2015-05-01 06:37:29 -07:00
2015-06-01 22:51:00 -07:00
.l-sub-section
2015-06-19 12:08:22 -07:00
h3 hostComponent
2015-06-01 22:51:00 -07:00
:markdown
2015-04-28 06:22:25 -07:00
.l-sub-section
2015-06-19 12:08:22 -07:00
h3 childRouter
2015-04-28 06:22:25 -07:00
pre.prettyprint
code.
2015-06-19 12:08:22 -07:00
childRouter(hostComponent: any)
2015-04-28 06:22:25 -07:00
:markdown
2015-05-01 06:37:29 -07:00
2015-06-19 12:08:22 -07:00
Constructs a child router. You probably don't need to use this unless you're writing a reusable
component.
2015-06-01 22:51:00 -07:00
2015-04-28 06:22:25 -07:00
.l-sub-section
2015-06-19 12:08:22 -07:00
h3 registerOutlet
2015-04-28 06:22:25 -07:00
2015-06-19 12:08:22 -07:00
pre.prettyprint
code.
registerOutlet(outlet: RouterOutlet)
2015-04-28 06:22:25 -07:00
:markdown
2015-05-01 06:37:29 -07:00
2015-06-19 12:08:22 -07:00
Register an object to notify of route changes. You probably don't need to use this unless
you're writing a reusable component.
2015-04-28 06:22:25 -07:00
2015-06-01 22:51:00 -07:00
2015-04-28 06:22:25 -07:00
.l-sub-section
2015-06-19 12:08:22 -07:00
h3 config
2015-04-28 06:22:25 -07:00
2015-06-19 12:08:22 -07:00
pre.prettyprint
code.
config(config: any)
2015-04-28 06:22:25 -07:00
:markdown
2015-05-01 06:37:29 -07:00
2015-06-19 12:08:22 -07:00
Dynamically update the routing configuration and trigger a navigation.
# Usage
```
router.config({ 'path': '/', 'component': IndexCmp});
```
Or:
```
router.config([
{ 'path': '/', 'component': IndexComp },
{ 'path': '/user/:id', 'component': UserComp },
]);
```
2015-04-28 06:22:25 -07:00
2015-06-01 22:51:00 -07:00
2015-04-28 06:22:25 -07:00
.l-sub-section
h3 navigate
pre.prettyprint
code.
2015-06-19 12:08:22 -07:00
navigate(url: string)
2015-04-28 06:22:25 -07:00
:markdown
2015-05-01 06:37:29 -07:00
2015-06-19 12:08:22 -07:00
Navigate to a URL. Returns a promise that resolves when navigation is complete.
2015-06-01 22:51:00 -07:00
If the given URL begins with a `/`, router will navigate absolutely.
If the given URL does not begin with `/`, the router will navigate relative to this component.
2015-04-28 06:22:25 -07:00
.l-sub-section
2015-06-19 12:08:22 -07:00
h3 subscribe
2015-04-28 06:22:25 -07:00
2015-06-19 12:08:22 -07:00
pre.prettyprint
code.
subscribe(onNext)
2015-04-28 06:22:25 -07:00
:markdown
2015-05-01 06:37:29 -07:00
2015-06-19 12:08:22 -07:00
Subscribe to URL updates from the router
2015-04-28 06:22:25 -07:00
2015-06-01 22:51:00 -07:00
2015-04-28 06:22:25 -07:00
.l-sub-section
2015-06-19 12:08:22 -07:00
h3 commit
2015-04-28 06:22:25 -07:00
2015-06-19 12:08:22 -07:00
pre.prettyprint
code.
commit(instruction: Instruction)
2015-04-28 06:22:25 -07:00
:markdown
2015-05-01 06:37:29 -07:00
2015-06-19 12:08:22 -07:00
Updates this router and all descendant routers according to the given instruction
2015-04-28 06:22:25 -07:00
2015-06-01 22:51:00 -07:00
2015-04-28 06:22:25 -07:00
.l-sub-section
2015-06-19 12:08:22 -07:00
h3 deactivate
2015-04-28 06:22:25 -07:00
pre.prettyprint
code.
2015-06-19 12:08:22 -07:00
deactivate()
2015-04-28 06:22:25 -07:00
:markdown
2015-05-01 06:37:29 -07:00
2015-06-19 12:08:22 -07:00
Removes the contents of this router's outlet and all descendant outlets
2015-06-01 22:51:00 -07:00
2015-04-28 06:22:25 -07:00
.l-sub-section
2015-06-19 12:08:22 -07:00
h3 recognize
2015-04-28 06:22:25 -07:00
pre.prettyprint
code.
2015-06-19 12:08:22 -07:00
recognize(url: string)
2015-04-28 06:22:25 -07:00
:markdown
2015-05-01 06:37:29 -07:00
2015-06-19 12:08:22 -07:00
Given a URL, returns an instruction representing the component graph
2015-06-01 22:51:00 -07:00
2015-04-28 06:22:25 -07:00
.l-sub-section
h3 renavigate
pre.prettyprint
code.
renavigate()
:markdown
2015-05-01 06:37:29 -07:00
2015-06-19 12:08:22 -07:00
Navigates to either the last URL successfully navigated to, or the last URL requested if the
router has yet to successfully navigate.
2015-06-01 22:51:00 -07:00
2015-04-28 06:22:25 -07:00
.l-sub-section
2015-06-19 12:08:22 -07:00
h3 generate
2015-04-28 06:22:25 -07:00
pre.prettyprint
code.
2015-06-19 12:08:22 -07:00
generate(name: string, params: StringMap<string, string>)
2015-04-28 06:22:25 -07:00
:markdown
2015-05-01 06:37:29 -07:00
2015-06-19 12:08:22 -07:00
Generate a URL from a component name and optional map of parameters. The URL is relative to the
app's base href.
2015-06-01 22:51:00 -07:00
2015-04-28 06:22:25 -07:00