angular-cn/public/docs/js/latest/api/router/Router-class.jade

321 lines
4.5 KiB
Plaintext
Raw Normal View History

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>
defined in <a href="https://github.com/angular/angular/tree/master/modules/angular2/src/router/router.js#L9-L232">angular2/src/router/router.js (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.
When the router navigates from a URL, it must first recognizes it and serialize it into an `Instruction`.
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-01 22:51:00 -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-01 22:51:00 -07:00
h3 activate
2015-04-28 06:22:25 -07:00
pre.prettyprint
code.
2015-06-01 22:51:00 -07:00
activate(instruction:Instruction)
2015-04-28 06:22:25 -07:00
:markdown
2015-06-01 22:51:00 -07:00
Recursively activate.
Calls the "activate" hook on descendant components.
2015-04-28 06:22:25 -07:00
.l-sub-section
h3 childRouter
pre.prettyprint
code.
2015-06-01 22:51:00 -07:00
childRouter(hostComponent:any)
2015-04-28 06:22:25 -07:00
:markdown
2015-04-28 06:22:25 -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
.l-sub-section
h3 commit
pre.prettyprint
code.
commit(instruction:Instruction)
:markdown
2015-04-28 06:22:25 -07:00
.l-sub-section
h3 config
pre.prettyprint
code.
2015-05-18 17:39:04 -07:00
config(config:any)
2015-04-28 06:22:25 -07:00
:markdown
2015-06-01 22:51:00 -07:00
Dynamically update the routing configuration and trigger a navigation.
2015-04-28 06:22:25 -07:00
2015-05-18 17:39:04 -07:00
# Usage
2015-04-28 06:22:25 -07:00
```
2015-05-18 17:39:04 -07:00
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
.l-sub-section
h3 deactivate
pre.prettyprint
code.
deactivate()
:markdown
Recursively remove all components contained by this router's outlets.
Calls deactivate hooks on all descendant components
2015-04-28 06:22:25 -07:00
.l-sub-section
h3 generate
pre.prettyprint
code.
2015-06-01 22:51:00 -07:00
generate(name:string, params:StringMap&lt;string, string&gt;)
2015-04-28 06:22:25 -07:00
:markdown
2015-04-28 06:22:25 -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
.l-sub-section
2015-05-18 17:39:04 -07:00
h3 hostComponent
2015-04-28 06:22:25 -07:00
:markdown
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 lastNavigationAttempt
:markdown
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.
navigate(url:string)
:markdown
2015-04-28 06:22:25 -07:00
Navigate to a URL. Returns a promise that resolves to the canonical URL for the route.
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
h3 navigating
:markdown
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 parent
:markdown
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 previousUrl
:markdown
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 recognize
pre.prettyprint
code.
recognize(url:string)
:markdown
2015-04-28 06:22:25 -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 registerOutlet
pre.prettyprint
code.
2015-06-01 22:51:00 -07:00
registerOutlet(outlet:RouterOutlet, name: string = &#39;default&#39;)
2015-04-28 06:22:25 -07:00
:markdown
2015-04-28 06:22:25 -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-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-04-28 06:22:25 -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
h3 subscribe
pre.prettyprint
code.
subscribe(onNext)
:markdown
2015-04-28 06:22:25 -07:00
Subscribe to URL updates from the router
2015-06-01 22:51:00 -07:00
2015-04-28 06:22:25 -07:00