/** * @license * Copyright Google Inc. All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ import { Location } from '@angular/common'; import { Compiler, Injector, NgModuleFactoryLoader, Type } from '@angular/core'; import { Observable } from 'rxjs'; import { QueryParamsHandling, Routes } from './config'; import { Event, NavigationTrigger } from './events'; import { RouteReuseStrategy } from './route_reuse_strategy'; import { ChildrenOutletContexts } from './router_outlet_context'; import { ActivatedRoute, RouterState, RouterStateSnapshot } from './router_state'; import { Params } from './shared'; import { UrlHandlingStrategy } from './url_handling_strategy'; import { UrlSerializer, UrlTree } from './url_tree'; import { Checks } from './utils/preactivation'; /** * @description * * Represents the extra options used during navigation. * * @publicApi */ export interface NavigationExtras { /** * Enables relative navigation from the current ActivatedRoute. * * Configuration: * * ``` * [{ * path: 'parent', * component: ParentComponent, * children: [{ * path: 'list', * component: ListComponent * },{ * path: 'child', * component: ChildComponent * }] * }] * ``` * * Navigate to list route from child route: * * ``` * @Component({...}) * class ChildComponent { * constructor(private router: Router, private route: ActivatedRoute) {} * * go() { * this.router.navigate(['../list'], { relativeTo: this.route }); * } * } * ``` */ relativeTo?: ActivatedRoute | null; /** * Sets query parameters to the URL. * * ``` * // Navigate to /results?page=1 * this.router.navigate(['/results'], { queryParams: { page: 1 } }); * ``` */ queryParams?: Params | null; /** * Sets the hash fragment for the URL. * * ``` * // Navigate to /results#top * this.router.navigate(['/results'], { fragment: 'top' }); * ``` */ fragment?: string; /** * Preserves the query parameters for the next navigation. * * deprecated, use `queryParamsHandling` instead * * ``` * // Preserve query params from /results?page=1 to /view?page=1 * this.router.navigate(['/view'], { preserveQueryParams: true }); * ``` * * @deprecated since v4 */ preserveQueryParams?: boolean; /** * config strategy to handle the query parameters for the next navigation. * * ``` * // from /results?page=1 to /view?page=1&page=2 * this.router.navigate(['/view'], { queryParams: { page: 2 }, queryParamsHandling: "merge" }); * ``` */ queryParamsHandling?: QueryParamsHandling | null; /** * Preserves the fragment for the next navigation * * ``` * // Preserve fragment from /results#top to /view#top * this.router.navigate(['/view'], { preserveFragment: true }); * ``` */ preserveFragment?: boolean; /** * Navigates without pushing a new state into history. * * ``` * // Navigate silently to /view * this.router.navigate(['/view'], { skipLocationChange: true }); * ``` */ skipLocationChange?: boolean; /** * Navigates while replacing the current state in history. * * ``` * // Navigate to /view * this.router.navigate(['/view'], { replaceUrl: true }); * ``` */ replaceUrl?: boolean; } /** * @description * * Error handler that is invoked when a navigation errors. * * If the handler returns a value, the navigation promise will be resolved with this value. * If the handler throws an exception, the navigation promise will be rejected with * the exception. * * @publicApi */ export declare type ErrorHandler = (error: any) => any; export declare type NavigationTransition = { id: number; currentUrlTree: UrlTree; currentRawUrl: UrlTree; extractedUrl: UrlTree; urlAfterRedirects: UrlTree; rawUrl: UrlTree; extras: NavigationExtras; resolve: any; reject: any; promise: Promise; source: NavigationTrigger; state: { navigationId: number; } | null; currentSnapshot: RouterStateSnapshot; targetSnapshot: RouterStateSnapshot | null; currentRouterState: RouterState; targetRouterState: RouterState | null; guards: Checks; guardsResult: boolean | null; }; /** * @description * * Provides the navigation and url manipulation capabilities. * * See `Routes` for more details and examples. * * @ngModule RouterModule * * @publicApi */ export declare class Router { private rootComponentType; private urlSerializer; private rootContexts; private location; config: Routes; private currentUrlTree; private rawUrlTree; private readonly transitions; private navigations; private locationSubscription; private navigationId; private configLoader; private ngModule; private console; private isNgZoneEnabled; readonly events: Observable; readonly routerState: RouterState; /** * Error handler that is invoked when a navigation errors. * * See `ErrorHandler` for more information. */ errorHandler: ErrorHandler; /** * Malformed uri error handler is invoked when `Router.parseUrl(url)` throws an * error due to containing an invalid character. The most common case would be a `%` sign * that's not encoded and is not part of a percent encoded sequence. */ malformedUriErrorHandler: (error: URIError, urlSerializer: UrlSerializer, url: string) => UrlTree; /** * Indicates if at least one navigation happened. */ navigated: boolean; private lastSuccessfulId; /** * Extracts and merges URLs. Used for AngularJS to Angular migrations. */ urlHandlingStrategy: UrlHandlingStrategy; routeReuseStrategy: RouteReuseStrategy; /** * Define what the router should do if it receives a navigation request to the current URL. * 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 * current URL. Default is 'ignore'. */ onSameUrlNavigation: 'reload' | 'ignore'; /** * Defines how the router merges params, data and resolved data from parent to child * routes. Available options are: * * - `'emptyOnly'`, the default, only inherits parent params for path-less or component-less * routes. * - `'always'`, enables unconditional inheritance of parent params. */ paramsInheritanceStrategy: 'emptyOnly' | 'always'; /** * Defines when the router updates the browser URL. The default behavior is to update after * successful navigation. However, some applications may prefer a mode where the URL gets * updated at the beginning of navigation. The most common use case would be updating the * URL early so if navigation fails, you can show an error message with the URL that failed. * Available options are: * * - `'deferred'`, the default, updates the browser URL after navigation has finished. * - `'eager'`, updates browser URL at the beginning of navigation. */ urlUpdateStrategy: 'deferred' | 'eager'; /** * See {@link RouterModule} for more information. */ relativeLinkResolution: 'legacy' | 'corrected'; /** * Creates the router service. */ constructor(rootComponentType: Type | null, urlSerializer: UrlSerializer, rootContexts: ChildrenOutletContexts, location: Location, injector: Injector, loader: NgModuleFactoryLoader, compiler: Compiler, config: Routes); private setupNavigations; private getTransition; private setTransition; /** * Sets up the location change listener and performs the initial navigation. */ initialNavigation(): void; /** * Sets up the location change listener. */ setUpLocationChangeListener(): void; /** The current url */ readonly url: string; /** * Resets the configuration used for navigation and generating links. * * @usageNotes * * ### Example * * ``` * router.resetConfig([ * { path: 'team/:id', component: TeamCmp, children: [ * { path: 'simple', component: SimpleCmp }, * { path: 'user/:name', component: UserCmp } * ]} * ]); * ``` */ resetConfig(config: Routes): void; /** @docsNotRequired */ ngOnDestroy(): void; /** Disposes of the router */ dispose(): void; /** * Applies an array of commands to the current url tree and creates a new url tree. * * When given an activate route, applies the given commands starting from the route. * When not given a route, applies the given command starting from the root. * * @usageNotes * * ### Example * * ``` * // create /team/33/user/11 * router.createUrlTree(['/team', 33, 'user', 11]); * * // create /team/33;expand=true/user/11 * router.createUrlTree(['/team', 33, {expand: true}, 'user', 11]); * * // you can collapse static segments like this (this works only with the first passed-in value): * router.createUrlTree(['/team/33/user', userId]); * * // If the first segment can contain slashes, and you do not want the router to split it, you * // can do the following: * * router.createUrlTree([{segmentPath: '/one/two'}]); * * // create /team/33/(user/11//right:chat) * router.createUrlTree(['/team', 33, {outlets: {primary: 'user/11', right: 'chat'}}]); * * // remove the right secondary node * router.createUrlTree(['/team', 33, {outlets: {primary: 'user/11', right: null}}]); * * // assuming the current url is `/team/33/user/11` and the route points to `user/11` * * // navigate to /team/33/user/11/details * router.createUrlTree(['details'], {relativeTo: route}); * * // navigate to /team/33/user/22 * router.createUrlTree(['../22'], {relativeTo: route}); * * // navigate to /team/44/user/22 * router.createUrlTree(['../../team/44/user/22'], {relativeTo: route}); * ``` */ createUrlTree(commands: any[], navigationExtras?: NavigationExtras): UrlTree; /** * Navigate based on the provided url. This navigation is always absolute. * * Returns a promise that: * - resolves to 'true' when navigation succeeds, * - resolves to 'false' when navigation fails, * - is rejected when an error happens. * * @usageNotes * * ### Example * * ``` * router.navigateByUrl("/team/33/user/11"); * * // Navigate without updating the URL * router.navigateByUrl("/team/33/user/11", { skipLocationChange: true }); * ``` * * Since `navigateByUrl()` takes an absolute URL as the first parameter, * it will not apply any delta to the current URL and ignores any properties * in the second parameter (the `NavigationExtras`) that would change the * provided URL. */ navigateByUrl(url: string | UrlTree, extras?: NavigationExtras): Promise; /** * Navigate based on the provided array of commands and a starting point. * If no starting route is provided, the navigation is absolute. * * Returns a promise that: * - resolves to 'true' when navigation succeeds, * - resolves to 'false' when navigation fails, * - is rejected when an error happens. * * @usageNotes * * ### Example * * ``` * router.navigate(['team', 33, 'user', 11], {relativeTo: route}); * * // Navigate without updating the URL * router.navigate(['team', 33, 'user', 11], {relativeTo: route, skipLocationChange: true}); * ``` * * The first parameter of `navigate()` is a delta to be applied to the current URL * or the one provided in the `relativeTo` property of the second parameter (the * `NavigationExtras`). */ navigate(commands: any[], extras?: NavigationExtras): Promise; /** Serializes a `UrlTree` into a string */ serializeUrl(url: UrlTree): string; /** Parses a string into a `UrlTree` */ parseUrl(url: string): UrlTree; /** Returns whether the url is activated */ isActive(url: string | UrlTree, exact: boolean): boolean; private removeEmptyProps; private processNavigations; private scheduleNavigation; private setBrowserUrl; private resetStateAndUrl; private resetUrlToCurrentUrlTree; }