docs(API): 翻译完了 RouterModule

This commit is contained in:
Zhicheng Wang 2018-09-01 20:55:38 +08:00
parent ea796c6952
commit 10ddb2449f
2 changed files with 171 additions and 2 deletions

View File

@ -26,7 +26,7 @@
[x] |core/ViewChild | 2,870 | 0.89 [x] |core/ViewChild | 2,870 | 0.89
[x] |core/Directive | 2,767 | 0.86 [x] |core/Directive | 2,767 | 0.86
[x] |router/Routes | 2,331 | 0.72 [x] |router/Routes | 2,331 | 0.72
[ ] |router/RouterModule | 2,227 | 0.69 [x] |router/RouterModule | 2,227 | 0.69
[x] |router/Route | 2,223 | 0.69 [x] |router/Route | 2,223 | 0.69
[ ] |common/http/HttpClientModule | 2,167 | 0.67 [ ] |common/http/HttpClientModule | 2,167 | 0.67
[ ] |core/ElementRef | 2,163 | 0.67 [ ] |core/ElementRef | 2,163 | 0.67

View File

@ -35,6 +35,7 @@ import {flatten} from './utils/collection';
* *
* Contains a list of directives * Contains a list of directives
* *
*
* *
*/ */
const ROUTER_DIRECTIVES = const ROUTER_DIRECTIVES =
@ -45,7 +46,7 @@ const ROUTER_DIRECTIVES =
* *
* Is used in DI to configure the router. * Is used in DI to configure the router.
* *
* * DI
*/ */
export const ROUTER_CONFIGURATION = new InjectionToken<ExtraOptions>('ROUTER_CONFIGURATION'); export const ROUTER_CONFIGURATION = new InjectionToken<ExtraOptions>('ROUTER_CONFIGURATION');
@ -86,16 +87,28 @@ export function routerNgProbeToken() {
* Since the router deals with a global shared resource--location, we cannot have * Since the router deals with a global shared resource--location, we cannot have
* more than one router service active. * more than one router service active.
* *
* RouterModule
* - location `Router`
*
* That is why there are two ways to create the module: `RouterModule.forRoot` and * That is why there are two ways to create the module: `RouterModule.forRoot` and
* `RouterModule.forChild`. * `RouterModule.forChild`.
* *
* `RouterModule.forRoot` `RouterModule.forChild`
*
* * `forRoot` creates a module that contains all the directives, the given routes, and the router * * `forRoot` creates a module that contains all the directives, the given routes, and the router
* service itself. * service itself.
*
* `forRoot` `Router`
*
* * `forChild` creates a module that contains all the directives and the given routes, but does not * * `forChild` creates a module that contains all the directives and the given routes, but does not
* include the router service. * include the router service.
* *
* `forChild` `Router`
*
* When registered at the root, the module should be used as follows * When registered at the root, the module should be used as follows
* *
*
*
* ``` * ```
* @NgModule({ * @NgModule({
* imports: [RouterModule.forRoot(ROUTES)] * imports: [RouterModule.forRoot(ROUTES)]
@ -105,6 +118,8 @@ export function routerNgProbeToken() {
* *
* For submodules and lazy loaded submodules the module should be used as follows: * For submodules and lazy loaded submodules the module should be used as follows:
* *
*
*
* ``` * ```
* @NgModule({ * @NgModule({
* imports: [RouterModule.forChild(ROUTES)] * imports: [RouterModule.forChild(ROUTES)]
@ -116,18 +131,26 @@ export function routerNgProbeToken() {
* *
* Adds router directives and providers. * Adds router directives and providers.
* *
*
*
* Managing state transitions is one of the hardest parts of building applications. This is * Managing state transitions is one of the hardest parts of building applications. This is
* especially true on the web, where you also need to ensure that the state is reflected in the URL. * especially true on the web, where you also need to ensure that the state is reflected in the URL.
* In addition, we often want to split applications into multiple bundles and load them on demand. * In addition, we often want to split applications into multiple bundles and load them on demand.
* Doing this transparently is not trivial. * Doing this transparently is not trivial.
* *
* Web URL
*
*
* The Angular router solves these problems. Using the router, you can declaratively specify * The Angular router solves these problems. Using the router, you can declaratively specify
* application states, manage state transitions while taking care of the URL, and load bundles on * application states, manage state transitions while taking care of the URL, and load bundles on
* demand. * demand.
* *
* Angular 使 URL
*
* [Read this developer guide](https://angular.io/docs/ts/latest/guide/router.html) to get an * [Read this developer guide](https://angular.io/docs/ts/latest/guide/router.html) to get an
* overview of how the router should be used. * overview of how the router should be used.
* *
* [](/guide/router) 使
* *
*/ */
@NgModule({ @NgModule({
@ -143,17 +166,43 @@ export class RouterModule {
* Creates a module with all the router providers and directives. It also optionally sets up an * Creates a module with all the router providers and directives. It also optionally sets up an
* application listener to perform an initial navigation. * application listener to perform an initial navigation.
* *
*
*
* Options (see `ExtraOptions`): * Options (see `ExtraOptions`):
*
* `ExtraOptions`
*
* * `enableTracing` makes the router log all its internal events to the console. * * `enableTracing` makes the router log all its internal events to the console.
*
* `enableTracing`
*
* * `useHash` enables the location strategy that uses the URL fragment instead of the history * * `useHash` enables the location strategy that uses the URL fragment instead of the history
* API. * API.
*
* `useHash` `LocationStrategy` URL `#` `history` API
*
* * `initialNavigation` disables the initial navigation. * * `initialNavigation` disables the initial navigation.
*
* `initialNavigation`
*
* * `errorHandler` provides a custom error handler. * * `errorHandler` provides a custom error handler.
*
* `errorHandler`
*
* * `preloadingStrategy` configures a preloading strategy (see `PreloadAllModules`). * * `preloadingStrategy` configures a preloading strategy (see `PreloadAllModules`).
*
* `preloadingStrategy` `PreloadAllModules`
*
* * `onSameUrlNavigation` configures how the router handles navigation to the current URL. See * * `onSameUrlNavigation` configures how the router handles navigation to the current URL. See
* `ExtraOptions` for more details. * `ExtraOptions` for more details.
*
* `onSameUrlNavigation` URL `ExtraOptions`
*
* * `paramsInheritanceStrategy` defines how the router merges params, data and resolved data * * `paramsInheritanceStrategy` defines how the router merges params, data and resolved data
* from parent to child routes. * from parent to child routes.
*
* `paramsInheritanceStrategy`
*
*/ */
static forRoot(routes: Routes, config?: ExtraOptions): ModuleWithProviders<RouterModule> { static forRoot(routes: Routes, config?: ExtraOptions): ModuleWithProviders<RouterModule> {
return { return {
@ -192,6 +241,8 @@ export class RouterModule {
/** /**
* Creates a module with all the router directives and a provider registering routes. * Creates a module with all the router directives and a provider registering routes.
*
*
*/ */
static forChild(routes: Routes): ModuleWithProviders<RouterModule> { static forChild(routes: Routes): ModuleWithProviders<RouterModule> {
return {ngModule: RouterModule, providers: [provideRoutes(routes)]}; return {ngModule: RouterModule, providers: [provideRoutes(routes)]};
@ -225,8 +276,12 @@ export function provideForRootGuard(router: Router): any {
* *
* Registers routes. * Registers routes.
* *
*
*
* ### Example * ### Example
* *
* ###
*
* ``` * ```
* @NgModule({ * @NgModule({
* imports: [RouterModule.forChild(ROUTES)], * imports: [RouterModule.forChild(ROUTES)],
@ -249,24 +304,47 @@ export function provideRoutes(routes: Routes): any {
* *
* Represents an option to configure when the initial navigation is performed. * Represents an option to configure when the initial navigation is performed.
* *
*
*
* * 'enabled' - the initial navigation starts before the root component is created. * * 'enabled' - the initial navigation starts before the root component is created.
* The bootstrap is blocked until the initial navigation is complete. * The bootstrap is blocked until the initial navigation is complete.
*
* 'enabled' -
*
* * 'disabled' - the initial navigation is not performed. The location listener is set up before * * 'disabled' - the initial navigation is not performed. The location listener is set up before
* the root component gets created. * the root component gets created.
*
* 'disabled' -
*
* * 'legacy_enabled'- the initial navigation starts after the root component has been created. * * 'legacy_enabled'- the initial navigation starts after the root component has been created.
* The bootstrap is not blocked until the initial navigation is complete. @deprecated * The bootstrap is not blocked until the initial navigation is complete. @deprecated
*
* 'legacy_enabled' - @deprecated
*
* * 'legacy_disabled'- the initial navigation is not performed. The location listener is set up * * 'legacy_disabled'- the initial navigation is not performed. The location listener is set up
* after @deprecated * after @deprecated
* the root component gets created. * the root component gets created.
*
* 'legacy_disabled' - @deprecated
*
* * `true` - same as 'legacy_enabled'. @deprecated since v4 * * `true` - same as 'legacy_enabled'. @deprecated since v4
*
* `true` - 'legacy_enabled'. @deprecated since v4
*
* * `false` - same as 'legacy_disabled'. @deprecated since v4 * * `false` - same as 'legacy_disabled'. @deprecated since v4
* *
* `false` - 'legacy_disabled'. @deprecated since v4
*
* The 'enabled' option should be used for applications unless there is a reason to have * The 'enabled' option should be used for applications unless there is a reason to have
* more control over when the router starts its initial navigation due to some complex * more control over when the router starts its initial navigation due to some complex
* initialization logic. In this case, 'disabled' should be used. * initialization logic. In this case, 'disabled' should be used.
* *
* 使 'enabled'使 'disabled'
*
* The 'legacy_enabled' and 'legacy_disabled' should not be used for new applications. * The 'legacy_enabled' and 'legacy_disabled' should not be used for new applications.
* *
* 使 'legacy_enabled' 'legacy_disabled'
*
* @experimental * @experimental
*/ */
export type InitialNavigation = export type InitialNavigation =
@ -277,31 +355,42 @@ export type InitialNavigation =
* *
* Represents options to configure the router. * Represents options to configure the router.
* *
*
* *
*/ */
export interface ExtraOptions { export interface ExtraOptions {
/** /**
* Makes the router log all its internal events to the console. * Makes the router log all its internal events to the console.
*
*
*/ */
enableTracing?: boolean; enableTracing?: boolean;
/** /**
* Enables the location strategy that uses the URL fragment instead of the history API. * Enables the location strategy that uses the URL fragment instead of the history API.
*
* `LocationStrategy` URL `#` `history` API
*/ */
useHash?: boolean; useHash?: boolean;
/** /**
* Disables the initial navigation. * Disables the initial navigation.
*
*
*/ */
initialNavigation?: InitialNavigation; initialNavigation?: InitialNavigation;
/** /**
* A custom error handler. * A custom error handler.
*
*
*/ */
errorHandler?: ErrorHandler; errorHandler?: ErrorHandler;
/** /**
* Configures a preloading strategy. See `PreloadAllModules`. * Configures a preloading strategy. See `PreloadAllModules`.
*
* `PreloadAllModules`
*/ */
preloadingStrategy?: any; preloadingStrategy?: any;
@ -310,22 +399,41 @@ export interface ExtraOptions {
* By default, the router will ignore this navigation. However, this prevents features such * By default, the router will ignore this navigation. However, this prevents features such
* as a "refresh" button. Use this option to configure the behavior when navigating to the * as a "refresh" button. Use this option to configure the behavior when navigating to the
* current URL. Default is 'ignore'. * current URL. Default is 'ignore'.
*
* URL
* "刷新"
* 使 URL 'ignore'
*/ */
onSameUrlNavigation?: 'reload'|'ignore'; onSameUrlNavigation?: 'reload'|'ignore';
/** /**
* Configures if the scroll position needs to be restored when navigating back. * Configures if the scroll position needs to be restored when navigating back.
* *
*
*
* * 'disabled'--does nothing (default). * * 'disabled'--does nothing (default).
*
* 'disabled' -
*
* * 'top'--set the scroll position to 0,0.. * * 'top'--set the scroll position to 0,0..
*
* 'top' - 0,0
*
* * 'enabled'--set the scroll position to the stored position. This option will be the default in * * 'enabled'--set the scroll position to the stored position. This option will be the default in
* the future. * the future.
* *
* 'enabled' -
*
* When enabled, the router store store scroll positions when navigating forward, and will * When enabled, the router store store scroll positions when navigating forward, and will
* restore the stored positions whe navigating back (popstate). When navigating forward, * restore the stored positions whe navigating back (popstate). When navigating forward,
* the scroll position will be set to [0, 0], or to the anchor if one is provided. * the scroll position will be set to [0, 0], or to the anchor if one is provided.
* *
* popstate [0, 0]
*
* You can implement custom scroll restoration behavior as follows. * You can implement custom scroll restoration behavior as follows.
*
*
*
* ```typescript * ```typescript
* class AppModule { * class AppModule {
* constructor(router: Router, viewportScroller: ViewportScroller, store: Store<AppState>) { * constructor(router: Router, viewportScroller: ViewportScroller, store: Store<AppState>) {
@ -346,6 +454,8 @@ export interface ExtraOptions {
* *
* You can also implement component-specific scrolling like this: * You can also implement component-specific scrolling like this:
* *
*
*
* ```typescript * ```typescript
* class ListComponent { * class ListComponent {
* list: any[]; * list: any[];
@ -367,20 +477,34 @@ export interface ExtraOptions {
/** /**
* Configures if the router should scroll to the element when the url has a fragment. * Configures if the router should scroll to the element when the url has a fragment.
* *
* url `#`
*
* * 'disabled'--does nothing (default). * * 'disabled'--does nothing (default).
*
* 'disabled' -
*
* * 'enabled'--scrolls to the element. This option will be the default in the future. * * 'enabled'--scrolls to the element. This option will be the default in the future.
* *
* 'enabled' -
*
* Anchor scrolling does not happen on 'popstate'. Instead, we restore the position * Anchor scrolling does not happen on 'popstate'. Instead, we restore the position
* that we stored or scroll to the top. * that we stored or scroll to the top.
*
* 'popstate'
*/ */
anchorScrolling?: 'disabled'|'enabled'; anchorScrolling?: 'disabled'|'enabled';
/** /**
* Configures the scroll offset the router will use when scrolling to an element. * Configures the scroll offset the router will use when scrolling to an element.
* *
* 使
*
* When given a tuple with two numbers, the router will always use the numbers. * When given a tuple with two numbers, the router will always use the numbers.
* When given a function, the router will invoke the function every time it restores scroll * When given a function, the router will invoke the function every time it restores scroll
* position. * position.
*
* 使
*
*/ */
scrollOffset?: [number, number]|(() => [number, number]); scrollOffset?: [number, number]|(() => [number, number]);
@ -388,9 +512,16 @@ export interface ExtraOptions {
* Defines how the router merges params, data and resolved data from parent to child * Defines how the router merges params, data and resolved data from parent to child
* routes. Available options are: * routes. Available options are:
* *
*
*
* - `'emptyOnly'`, the default, only inherits parent params for path-less or component-less * - `'emptyOnly'`, the default, only inherits parent params for path-less or component-less
* routes. * routes.
*
* `'emptyOnly'`
*
* - `'always'`, enables unconditional inheritance of parent params. * - `'always'`, enables unconditional inheritance of parent params.
*
* `'always'`
*/ */
paramsInheritanceStrategy?: 'emptyOnly'|'always'; paramsInheritanceStrategy?: 'emptyOnly'|'always';
@ -399,9 +530,20 @@ export interface ExtraOptions {
* invalid character sequences. The default implementation is to redirect to the root url dropping * invalid character sequences. The default implementation is to redirect to the root url dropping
* any path or param info. This function passes three parameters: * any path or param info. This function passes three parameters:
* *
* URI encodeURI
*
* - `'URIError'` - Error thrown when parsing a bad URL * - `'URIError'` - Error thrown when parsing a bad URL
*
* `'URIError'` - URL
*
* - `'UrlSerializer'` - UrlSerializer thats configured with the router. * - `'UrlSerializer'` - UrlSerializer thats configured with the router.
*
* `'UrlSerializer'` - UrlSerializer
*
* - `'url'` - The malformed URL that caused the URIError * - `'url'` - The malformed URL that caused the URIError
*
* `'url'` - URIError URL
*
* */ * */
malformedUriErrorHandler?: malformedUriErrorHandler?:
(error: URIError, urlSerializer: UrlSerializer, url: string) => UrlTree; (error: URIError, urlSerializer: UrlSerializer, url: string) => UrlTree;
@ -413,8 +555,17 @@ export interface ExtraOptions {
* URL early so if navigation fails, you can show an error message with the URL that failed. * URL early so if navigation fails, you can show an error message with the URL that failed.
* Available options are: * Available options are:
* *
* URL
* URL URL
*
*
* - `'deferred'`, the default, updates the browser URL after navigation has finished. * - `'deferred'`, the default, updates the browser URL after navigation has finished.
*
* `'deferred'` URL
*
* - `'eager'`, updates browser URL at the beginning of navigation. * - `'eager'`, updates browser URL at the beginning of navigation.
*
* `'eager'` URL
*/ */
urlUpdateStrategy?: 'deferred'|'eager'; urlUpdateStrategy?: 'deferred'|'eager';
@ -422,6 +573,8 @@ export interface ExtraOptions {
* Enables a bug fix that corrects relative link resolution in components with empty paths. * Enables a bug fix that corrects relative link resolution in components with empty paths.
* Example: * Example:
* *
* BUG
*
* ``` * ```
* const routes = [ * const routes = [
* { * {
@ -437,14 +590,20 @@ export interface ExtraOptions {
* *
* From the `ContainerComponent`, this will not work: * From the `ContainerComponent`, this will not work:
* *
* `ContainerComponent`
*
* `<a [routerLink]="['./a']">Link to A</a>` * `<a [routerLink]="['./a']">Link to A</a>`
* *
* However, this will work: * However, this will work:
* *
*
*
* `<a [routerLink]="['../a']">Link to A</a>` * `<a [routerLink]="['../a']">Link to A</a>`
* *
* In other words, you're required to use `../` rather than `./`. The current default in v6 * In other words, you're required to use `../` rather than `./`. The current default in v6
* is `legacy`, and this option will be removed in v7 to default to the corrected behavior. * is `legacy`, and this option will be removed in v7 to default to the corrected behavior.
*
* 使 `../` `./` v6 `legacy` v7
*/ */
relativeLinkResolution?: 'legacy'|'corrected'; relativeLinkResolution?: 'legacy'|'corrected';
} }
@ -509,13 +668,21 @@ export function rootRoute(router: Router): ActivatedRoute {
/** /**
* To initialize the router properly we need to do in two steps: * To initialize the router properly we need to do in two steps:
* *
*
*
* We need to start the navigation in a APP_INITIALIZER to block the bootstrap if * We need to start the navigation in a APP_INITIALIZER to block the bootstrap if
* a resolver or a guards executes asynchronously. Second, we need to actually run * a resolver or a guards executes asynchronously. Second, we need to actually run
* activation in a BOOTSTRAP_LISTENER. We utilize the afterPreactivation * activation in a BOOTSTRAP_LISTENER. We utilize the afterPreactivation
* hook provided by the router to do that. * hook provided by the router to do that.
* *
* APP_INITIALIZER 便
* BOOTSTRAP_LISTENER
* `afterPreactivation`
*
* The router navigation starts, reaches the point when preactivation is done, and then * The router navigation starts, reaches the point when preactivation is done, and then
* pauses. It waits for the hook to be resolved. We then resolve it only in a bootstrap listener. * pauses. It waits for the hook to be resolved. We then resolve it only in a bootstrap listener.
*
* `preactivation`
*/ */
@Injectable() @Injectable()
export class RouterInitializer { export class RouterInitializer {
@ -607,6 +774,8 @@ export function getBootstrapListener(r: RouterInitializer) {
/** /**
* A token for the router initializer that will be called after the app is bootstrapped. * A token for the router initializer that will be called after the app is bootstrapped.
* *
*
*
* @experimental * @experimental
*/ */
export const ROUTER_INITIALIZER = export const ROUTER_INITIALIZER =