From 549f2254b472b7a5d03343e6aa151b690827ef4f Mon Sep 17 00:00:00 2001 From: Yuan Gao Date: Mon, 11 Sep 2017 12:39:44 -0700 Subject: [PATCH] refactor(core): remove getters for packages/animations, language-service, platform-browser, router (#19151) PR Close #19151 --- .../src/render/transition_animation_engine.ts | 6 +- .../web_animations/web_animations_player.ts | 26 ++++---- .../src/players/animation_group_player.ts | 30 ++++----- packages/animations/src/version.ts | 15 +++-- .../language-service/src/locate_symbol.ts | 5 +- .../src/compiler_factory.ts | 6 +- .../location/browser_platform_location.ts | 18 +++--- packages/platform-server/src/location.ts | 24 +++---- .../src/directives/router_link_active.ts | 9 +-- .../router/src/directives/router_outlet.ts | 2 +- packages/router/src/pre_activation.ts | 12 ++-- packages/router/src/router.ts | 62 +++++++++---------- packages/router/src/router_state.ts | 13 ++-- 13 files changed, 109 insertions(+), 119 deletions(-) diff --git a/packages/animations/browser/src/render/transition_animation_engine.ts b/packages/animations/browser/src/render/transition_animation_engine.ts index fe779f3bb8..dcaeccb4f7 100644 --- a/packages/animations/browser/src/render/transition_animation_engine.ts +++ b/packages/animations/browser/src/render/transition_animation_engine.ts @@ -1305,7 +1305,7 @@ export class TransitionAnimationPlayer implements AnimationPlayer { private _containsRealPlayer = false; private _queuedCallbacks: {[name: string]: (() => any)[]} = {}; - private _destroyed = false; + public readonly destroyed = false; public parentPlayer: AnimationPlayer; public markedForDestroy: boolean = false; @@ -1314,8 +1314,6 @@ export class TransitionAnimationPlayer implements AnimationPlayer { get queued() { return this._containsRealPlayer == false; } - get destroyed() { return this._destroyed; } - setRealPlayer(player: AnimationPlayer) { if (this._containsRealPlayer) return; @@ -1368,7 +1366,7 @@ export class TransitionAnimationPlayer implements AnimationPlayer { finish(): void { this._player.finish(); } destroy(): void { - this._destroyed = true; + (this as{destroyed: boolean}).destroyed = true; this._player.destroy(); } diff --git a/packages/animations/browser/src/render/web_animations/web_animations_player.ts b/packages/animations/browser/src/render/web_animations/web_animations_player.ts index 37b2d626eb..dc7396f4f9 100644 --- a/packages/animations/browser/src/render/web_animations/web_animations_player.ts +++ b/packages/animations/browser/src/render/web_animations/web_animations_player.ts @@ -15,7 +15,6 @@ export class WebAnimationsPlayer implements AnimationPlayer { private _onDoneFns: Function[] = []; private _onStartFns: Function[] = []; private _onDestroyFns: Function[] = []; - private _player: DOMAnimation; private _duration: number; private _delay: number; private _initialized = false; @@ -23,6 +22,8 @@ export class WebAnimationsPlayer implements AnimationPlayer { private _started = false; private _destroyed = false; private _finalKeyframe: {[key: string]: string | number}; + + public readonly domPlayer: DOMAnimation; public time = 0; public parentPlayer: AnimationPlayer|null = null; @@ -86,9 +87,10 @@ export class WebAnimationsPlayer implements AnimationPlayer { } } - this._player = this._triggerWebAnimation(this.element, keyframes, this.options); + (this as{domPlayer: DOMAnimation}).domPlayer = + this._triggerWebAnimation(this.element, keyframes, this.options); this._finalKeyframe = keyframes.length ? keyframes[keyframes.length - 1] : {}; - this._player.addEventListener('finish', () => this._onFinish()); + this.domPlayer.addEventListener('finish', () => this._onFinish()); } private _preparePlayerBeforeStart() { @@ -96,7 +98,7 @@ export class WebAnimationsPlayer implements AnimationPlayer { if (this._delay) { this._resetDomPlayerState(); } else { - this._player.pause(); + this.domPlayer.pause(); } } @@ -107,8 +109,6 @@ export class WebAnimationsPlayer implements AnimationPlayer { return element['animate'](keyframes, options) as DOMAnimation; } - get domPlayer() { return this._player; } - onStart(fn: () => void): void { this._onStartFns.push(fn); } onDone(fn: () => void): void { this._onDoneFns.push(fn); } @@ -122,18 +122,18 @@ export class WebAnimationsPlayer implements AnimationPlayer { this._onStartFns = []; this._started = true; } - this._player.play(); + this.domPlayer.play(); } pause(): void { this.init(); - this._player.pause(); + this.domPlayer.pause(); } finish(): void { this.init(); this._onFinish(); - this._player.finish(); + this.domPlayer.finish(); } reset(): void { @@ -144,8 +144,8 @@ export class WebAnimationsPlayer implements AnimationPlayer { } private _resetDomPlayerState() { - if (this._player) { - this._player.cancel(); + if (this.domPlayer) { + this.domPlayer.cancel(); } } @@ -166,9 +166,9 @@ export class WebAnimationsPlayer implements AnimationPlayer { } } - setPosition(p: number): void { this._player.currentTime = p * this.time; } + setPosition(p: number): void { this.domPlayer.currentTime = p * this.time; } - getPosition(): number { return this._player.currentTime / this.time; } + getPosition(): number { return this.domPlayer.currentTime / this.time; } get totalTime(): number { return this._delay + this._duration; } diff --git a/packages/animations/src/players/animation_group_player.ts b/packages/animations/src/players/animation_group_player.ts index d1b0aae86f..b67c4ef82f 100644 --- a/packages/animations/src/players/animation_group_player.ts +++ b/packages/animations/src/players/animation_group_player.ts @@ -19,17 +19,19 @@ export class AnimationGroupPlayer implements AnimationPlayer { public parentPlayer: AnimationPlayer|null = null; public totalTime: number = 0; + public readonly players: AnimationPlayer[]; - constructor(private _players: AnimationPlayer[]) { + constructor(_players: AnimationPlayer[]) { + this.players = _players; let doneCount = 0; let destroyCount = 0; let startCount = 0; - const total = this._players.length; + const total = this.players.length; if (total == 0) { scheduleMicroTask(() => this._onFinish()); } else { - this._players.forEach(player => { + this.players.forEach(player => { player.parentPlayer = this; player.onDone(() => { if (++doneCount >= total) { @@ -49,7 +51,7 @@ export class AnimationGroupPlayer implements AnimationPlayer { }); } - this.totalTime = this._players.reduce((time, player) => Math.max(time, player.totalTime), 0); + this.totalTime = this.players.reduce((time, player) => Math.max(time, player.totalTime), 0); } private _onFinish() { @@ -60,7 +62,7 @@ export class AnimationGroupPlayer implements AnimationPlayer { } } - init(): void { this._players.forEach(player => player.init()); } + init(): void { this.players.forEach(player => player.init()); } onStart(fn: () => void): void { this._onStartFns.push(fn); } @@ -83,16 +85,16 @@ export class AnimationGroupPlayer implements AnimationPlayer { this.init(); } this._onStart(); - this._players.forEach(player => player.play()); + this.players.forEach(player => player.play()); } - pause(): void { this._players.forEach(player => player.pause()); } + pause(): void { this.players.forEach(player => player.pause()); } - restart(): void { this._players.forEach(player => player.restart()); } + restart(): void { this.players.forEach(player => player.restart()); } finish(): void { this._onFinish(); - this._players.forEach(player => player.finish()); + this.players.forEach(player => player.finish()); } destroy(): void { this._onDestroy(); } @@ -101,14 +103,14 @@ export class AnimationGroupPlayer implements AnimationPlayer { if (!this._destroyed) { this._destroyed = true; this._onFinish(); - this._players.forEach(player => player.destroy()); + this.players.forEach(player => player.destroy()); this._onDestroyFns.forEach(fn => fn()); this._onDestroyFns = []; } } reset(): void { - this._players.forEach(player => player.reset()); + this.players.forEach(player => player.reset()); this._destroyed = false; this._finished = false; this._started = false; @@ -116,7 +118,7 @@ export class AnimationGroupPlayer implements AnimationPlayer { setPosition(p: number): void { const timeAtPosition = p * this.totalTime; - this._players.forEach(player => { + this.players.forEach(player => { const position = player.totalTime ? Math.min(1, timeAtPosition / player.totalTime) : 1; player.setPosition(position); }); @@ -124,15 +126,13 @@ export class AnimationGroupPlayer implements AnimationPlayer { getPosition(): number { let min = 0; - this._players.forEach(player => { + this.players.forEach(player => { const p = player.getPosition(); min = Math.min(p, min); }); return min; } - get players(): AnimationPlayer[] { return this._players; } - beforeDestroy(): void { this.players.forEach(player => { if (player.beforeDestroy) { diff --git a/packages/animations/src/version.ts b/packages/animations/src/version.ts index 68ff8e5b3e..929a53ed3e 100644 --- a/packages/animations/src/version.ts +++ b/packages/animations/src/version.ts @@ -18,13 +18,16 @@ * @stable */ export class Version { - constructor(public full: string) {} + public readonly major: string; + public readonly minor: string; + public readonly patch: string; - get major(): string { return this.full.split('.')[0]; } - - get minor(): string { return this.full.split('.')[1]; } - - get patch(): string { return this.full.split('.').slice(2).join('.'); } + constructor(public full: string) { + const splits = full.split('.'); + this.major = splits[0]; + this.minor = splits[1]; + this.patch = splits.slice(2).join('.'); + } } /** diff --git a/packages/language-service/src/locate_symbol.ts b/packages/language-service/src/locate_symbol.ts index 42dce460a4..c1e9a0e161 100644 --- a/packages/language-service/src/locate_symbol.ts +++ b/packages/language-service/src/locate_symbol.ts @@ -169,12 +169,11 @@ function invertMap(obj: {[name: string]: string}): {[name: string]: string} { * Wrap a symbol and change its kind to component. */ class OverrideKindSymbol implements Symbol { - constructor(private sym: Symbol, private kindOverride: string) {} + public readonly kind: string; + constructor(private sym: Symbol, kindOverride: string) { this.kind = kindOverride; } get name(): string { return this.sym.name; } - get kind(): string { return this.kindOverride; } - get language(): string { return this.sym.language; } get type(): Symbol|undefined { return this.sym.type; } diff --git a/packages/platform-browser-dynamic/src/compiler_factory.ts b/packages/platform-browser-dynamic/src/compiler_factory.ts index d3c477cee9..9da0a7900f 100644 --- a/packages/platform-browser-dynamic/src/compiler_factory.ts +++ b/packages/platform-browser-dynamic/src/compiler_factory.ts @@ -32,8 +32,9 @@ const baseHtmlParser = new InjectionToken('HtmlParser'); export class CompilerImpl implements Compiler { private _delegate: JitCompiler; + public readonly injector: Injector; constructor( - private _injector: Injector, private _metadataResolver: CompileMetadataResolver, + injector: Injector, private _metadataResolver: CompileMetadataResolver, templateParser: TemplateParser, styleCompiler: StyleCompiler, viewCompiler: ViewCompiler, ngModuleCompiler: NgModuleCompiler, summaryResolver: SummaryResolver>, compileReflector: CompileReflector, compilerConfig: CompilerConfig, console: Console) { @@ -41,10 +42,9 @@ export class CompilerImpl implements Compiler { _metadataResolver, templateParser, styleCompiler, viewCompiler, ngModuleCompiler, summaryResolver, compileReflector, compilerConfig, console, this.getExtraNgModuleProviders.bind(this)); + this.injector = injector; } - get injector(): Injector { return this._injector; } - private getExtraNgModuleProviders() { return [this._metadataResolver.getProviderMetadata( new ProviderMeta(Compiler, {useValue: this}))]; diff --git a/packages/platform-browser/src/browser/location/browser_platform_location.ts b/packages/platform-browser/src/browser/location/browser_platform_location.ts index 45f6ef4a15..523ef0b8cf 100644 --- a/packages/platform-browser/src/browser/location/browser_platform_location.ts +++ b/packages/platform-browser/src/browser/location/browser_platform_location.ts @@ -23,7 +23,7 @@ import {supportsState} from './history'; */ @Injectable() export class BrowserPlatformLocation extends PlatformLocation { - private _location: Location; + public readonly location: Location; private _history: History; constructor(@Inject(DOCUMENT) private _doc: any) { @@ -34,12 +34,10 @@ export class BrowserPlatformLocation extends PlatformLocation { // This is moved to its own method so that `MockPlatformLocationStrategy` can overwrite it /** @internal */ _init() { - this._location = getDOM().getLocation(); + (this as{location: Location}).location = getDOM().getLocation(); this._history = getDOM().getHistory(); } - get location(): Location { return this._location; } - getBaseHrefFromDOM(): string { return getDOM().getBaseHref(this._doc) !; } onPopState(fn: LocationChangeListener): void { @@ -50,16 +48,16 @@ export class BrowserPlatformLocation extends PlatformLocation { getDOM().getGlobalEventTarget(this._doc, 'window').addEventListener('hashchange', fn, false); } - get pathname(): string { return this._location.pathname; } - get search(): string { return this._location.search; } - get hash(): string { return this._location.hash; } - set pathname(newPath: string) { this._location.pathname = newPath; } + get pathname(): string { return this.location.pathname; } + get search(): string { return this.location.search; } + get hash(): string { return this.location.hash; } + set pathname(newPath: string) { this.location.pathname = newPath; } pushState(state: any, title: string, url: string): void { if (supportsState()) { this._history.pushState(state, title, url); } else { - this._location.hash = url; + this.location.hash = url; } } @@ -67,7 +65,7 @@ export class BrowserPlatformLocation extends PlatformLocation { if (supportsState()) { this._history.replaceState(state, title, url); } else { - this._location.hash = url; + this.location.hash = url; } } diff --git a/packages/platform-server/src/location.ts b/packages/platform-server/src/location.ts index db6611a03e..b4983799cb 100644 --- a/packages/platform-server/src/location.ts +++ b/packages/platform-server/src/location.ts @@ -29,9 +29,9 @@ function parseUrl(urlStr: string): {pathname: string, search: string, hash: stri */ @Injectable() export class ServerPlatformLocation implements PlatformLocation { - private _path: string = '/'; - private _search: string = ''; - private _hash: string = ''; + public readonly pathname: string = '/'; + public readonly search: string = ''; + public readonly hash: string = ''; private _hashUpdate = new Subject(); constructor( @@ -39,9 +39,9 @@ export class ServerPlatformLocation implements PlatformLocation { const config = _config as PlatformConfig | null; if (!!config && !!config.url) { const parsedUrl = parseUrl(config.url); - this._path = parsedUrl.pathname; - this._search = parsedUrl.search; - this._hash = parsedUrl.hash; + this.pathname = parsedUrl.pathname; + this.search = parsedUrl.search; + this.hash = parsedUrl.hash; } } @@ -54,18 +54,14 @@ export class ServerPlatformLocation implements PlatformLocation { onHashChange(fn: LocationChangeListener): void { this._hashUpdate.subscribe(fn); } - get pathname(): string { return this._path; } - get search(): string { return this._search; } - get hash(): string { return this._hash; } - get url(): string { return `${this.pathname}${this.search}${this.hash}`; } private setHash(value: string, oldUrl: string) { - if (this._hash === value) { + if (this.hash === value) { // Don't fire events if the hash has not changed. return; } - this._hash = value; + (this as{hash: string}).hash = value; const newUrl = this.url; scheduleMicroTask( () => this._hashUpdate.next({ type: 'hashchange', oldUrl, newUrl } as LocationChangeEvent)); @@ -74,8 +70,8 @@ export class ServerPlatformLocation implements PlatformLocation { replaceState(state: any, title: string, newUrl: string): void { const oldUrl = this.url; const parsedUrl = parseUrl(newUrl); - this._path = parsedUrl.pathname; - this._search = parsedUrl.search; + (this as{pathname: string}).pathname = parsedUrl.pathname; + (this as{search: string}).search = parsedUrl.search; this.setHash(parsedUrl.hash, oldUrl); } diff --git a/packages/router/src/directives/router_link_active.ts b/packages/router/src/directives/router_link_active.ts index 97d9d8d53a..07c6e09217 100644 --- a/packages/router/src/directives/router_link_active.ts +++ b/packages/router/src/directives/router_link_active.ts @@ -86,7 +86,7 @@ export class RouterLinkActive implements OnChanges, private classes: string[] = []; private subscription: Subscription; - private active: boolean = false; + public readonly isActive: boolean = false; @Input() routerLinkActiveOptions: {exact: boolean} = {exact: false}; @@ -100,7 +100,6 @@ export class RouterLinkActive implements OnChanges, }); } - get isActive(): boolean { return this.active; } ngAfterContentInit(): void { this.links.changes.subscribe(_ => this.update()); @@ -122,7 +121,7 @@ export class RouterLinkActive implements OnChanges, const hasActiveLinks = this.hasActiveLinks(); // react only when status has changed to prevent unnecessary dom updates - if (this.active !== hasActiveLinks) { + if (this.isActive !== hasActiveLinks) { this.classes.forEach((c) => { if (hasActiveLinks) { this.renderer.addClass(this.element.nativeElement, c); @@ -130,7 +129,9 @@ export class RouterLinkActive implements OnChanges, this.renderer.removeClass(this.element.nativeElement, c); } }); - Promise.resolve(hasActiveLinks).then(active => this.active = active); + Promise.resolve(hasActiveLinks).then(active => (this as{ + isActive: boolean + }).isActive = active); } } diff --git a/packages/router/src/directives/router_outlet.ts b/packages/router/src/directives/router_outlet.ts index 4e6420357b..01b1a5e85d 100644 --- a/packages/router/src/directives/router_outlet.ts +++ b/packages/router/src/directives/router_outlet.ts @@ -128,7 +128,7 @@ export class RouterOutlet implements OnDestroy, OnInit { } this._activatedRoute = activatedRoute; const snapshot = activatedRoute._futureSnapshot; - const component = snapshot._routeConfig !.component; + const component = snapshot.routeConfig !.component; resolver = resolver || this.resolver; const factory = resolver.resolveComponentFactory(component); const childContexts = this.parentContexts.getOrCreateContext(this.name).children; diff --git a/packages/router/src/pre_activation.ts b/packages/router/src/pre_activation.ts index 64151dfb73..ba20b1f899 100644 --- a/packages/router/src/pre_activation.ts +++ b/packages/router/src/pre_activation.ts @@ -108,9 +108,9 @@ export class PreActivation { const context = parentContexts ? parentContexts.getContext(futureNode.value.outlet) : null; // reusing the node - if (curr && future._routeConfig === curr._routeConfig) { + if (curr && future.routeConfig === curr.routeConfig) { const shouldRunGuardsAndResolvers = this.shouldRunGuardsAndResolvers( - curr, future, future._routeConfig !.runGuardsAndResolvers); + curr, future, future.routeConfig !.runGuardsAndResolvers); if (shouldRunGuardsAndResolvers) { this.canActivateChecks.push(new CanActivate(futurePath)); } else { @@ -241,7 +241,7 @@ export class PreActivation { } private runCanActivate(future: ActivatedRouteSnapshot): Observable { - const canActivate = future._routeConfig ? future._routeConfig.canActivate : null; + const canActivate = future.routeConfig ? future.routeConfig.canActivate : null; if (!canActivate || canActivate.length === 0) return of (true); const obs = map.call(from(canActivate), (c: any) => { const guard = this.getToken(c, future); @@ -281,14 +281,14 @@ export class PreActivation { private extractCanActivateChild(p: ActivatedRouteSnapshot): {node: ActivatedRouteSnapshot, guards: any[]}|null { - const canActivateChild = p._routeConfig ? p._routeConfig.canActivateChild : null; + const canActivateChild = p.routeConfig ? p.routeConfig.canActivateChild : null; if (!canActivateChild || canActivateChild.length === 0) return null; return {node: p, guards: canActivateChild}; } private runCanDeactivate(component: Object|null, curr: ActivatedRouteSnapshot): Observable { - const canDeactivate = curr && curr._routeConfig ? curr._routeConfig.canDeactivate : null; + const canDeactivate = curr && curr.routeConfig ? curr.routeConfig.canDeactivate : null; if (!canDeactivate || canDeactivate.length === 0) return of (true); const canDeactivate$ = mergeMap.call(from(canDeactivate), (c: any) => { const guard = this.getToken(c, curr); @@ -351,7 +351,7 @@ function closestLoadedConfig(snapshot: ActivatedRouteSnapshot): LoadedRouterConf if (!snapshot) return null; for (let s = snapshot.parent; s; s = s.parent) { - const route = s._routeConfig; + const route = s.routeConfig; if (route && route._loadedConfig) return route._loadedConfig; } diff --git a/packages/router/src/router.ts b/packages/router/src/router.ts index 4a5b8426ef..fd553b4cbc 100644 --- a/packages/router/src/router.ts +++ b/packages/router/src/router.ts @@ -200,16 +200,16 @@ function defaultRouterHook(snapshot: RouterStateSnapshot): Observable { export class Router { private currentUrlTree: UrlTree; private rawUrlTree: UrlTree; - private navigations = new BehaviorSubject(null !); - private routerEvents = new Subject(); - private currentRouterState: RouterState; private locationSubscription: Subscription; private navigationId: number = 0; private configLoader: RouterConfigLoader; private ngModule: NgModuleRef; + public readonly events: Observable = new Subject(); + public readonly routerState: RouterState; + /** * Error handler that is invoked when a navigation errors. * @@ -259,7 +259,7 @@ export class Router { this.rawUrlTree = this.currentUrlTree; this.configLoader = new RouterConfigLoader(loader, compiler, onLoadStart, onLoadEnd); - this.currentRouterState = createEmptyState(this.currentUrlTree, this.rootComponentType); + this.routerState = createEmptyState(this.currentUrlTree, this.rootComponentType); this.processNavigations(); } @@ -271,7 +271,7 @@ export class Router { this.rootComponentType = rootComponentType; // TODO: vsavkin router 4.0 should make the root component set to null // this will simplify the lifecycle of the router. - this.currentRouterState.root.component = this.rootComponentType; + this.routerState.root.component = this.rootComponentType; } /** @@ -299,17 +299,11 @@ export class Router { } } - /** The current route state */ - get routerState(): RouterState { return this.currentRouterState; } - /** The current url */ get url(): string { return this.serializeUrl(this.currentUrlTree); } - /** An observable of router events */ - get events(): Observable { return this.routerEvents; } - /** @internal */ - triggerEvent(e: Event): void { this.routerEvents.next(e); } + triggerEvent(e: Event): void { (this.events as Subject).next(e); } /** * Resets the configuration used for navigation and generating links. @@ -552,7 +546,7 @@ export class Router { const urlTransition = !this.navigated || url.toString() !== this.currentUrlTree.toString(); if (urlTransition && this.urlHandlingStrategy.shouldProcessUrl(rawUrl)) { - this.routerEvents.next(new NavigationStart(id, this.serializeUrl(url))); + (this.events as Subject).next(new NavigationStart(id, this.serializeUrl(url))); Promise.resolve() .then( (_) => this.runNavigate( @@ -564,7 +558,7 @@ export class Router { } else if ( urlTransition && this.rawUrlTree && this.urlHandlingStrategy.shouldProcessUrl(this.rawUrlTree)) { - this.routerEvents.next(new NavigationStart(id, this.serializeUrl(url))); + (this.events as Subject).next(new NavigationStart(id, this.serializeUrl(url))); Promise.resolve() .then( (_) => this.runNavigate( @@ -583,9 +577,10 @@ export class Router { id: number, precreatedState: RouterStateSnapshot|null): Promise { if (id !== this.navigationId) { this.location.go(this.urlSerializer.serialize(this.currentUrlTree)); - this.routerEvents.next(new NavigationCancel( - id, this.serializeUrl(url), - `Navigation ID ${id} is not equal to the current navigation id ${this.navigationId}`)); + (this.events as Subject) + .next(new NavigationCancel( + id, this.serializeUrl(url), + `Navigation ID ${id} is not equal to the current navigation id ${this.navigationId}`)); return Promise.resolve(false); } @@ -604,8 +599,9 @@ export class Router { this.rootComponentType, this.config, appliedUrl, this.serializeUrl(appliedUrl)), (snapshot: any) => { - this.routerEvents.next(new RoutesRecognized( - id, this.serializeUrl(url), this.serializeUrl(appliedUrl), snapshot)); + (this.events as Subject) + .next(new RoutesRecognized( + id, this.serializeUrl(url), this.serializeUrl(appliedUrl), snapshot)); return {appliedUrl, snapshot}; }); @@ -627,7 +623,7 @@ export class Router { ({appliedUrl, snapshot}: {appliedUrl: string, snapshot: RouterStateSnapshot}) => { const moduleInjector = this.ngModule.injector; preActivation = new PreActivation( - snapshot, this.currentRouterState.snapshot, moduleInjector, + snapshot, this.routerState.snapshot, moduleInjector, (evt: Event) => this.triggerEvent(evt)); preActivation.initalize(this.rootContexts); return {appliedUrl, snapshot}; @@ -676,8 +672,7 @@ export class Router { const routerState$ = map.call(preactivationDone$, ({appliedUrl, snapshot, shouldActivate}: any) => { if (shouldActivate) { - const state = - createRouterState(this.routeReuseStrategy, snapshot, this.currentRouterState); + const state = createRouterState(this.routeReuseStrategy, snapshot, this.routerState); return {appliedUrl, state, shouldActivate}; } else { return {appliedUrl, state: null, shouldActivate}; @@ -688,7 +683,7 @@ export class Router { // applied the new router state // this operation has side effects let navigationIsSuccessful: boolean; - const storedState = this.currentRouterState; + const storedState = this.routerState; const storedUrl = this.currentUrlTree; routerState$ @@ -701,7 +696,7 @@ export class Router { this.currentUrlTree = appliedUrl; this.rawUrlTree = this.urlHandlingStrategy.merge(this.currentUrlTree, rawUrl); - this.currentRouterState = state; + (this as{routerState: RouterState}).routerState = state; if (!shouldPreventPushState) { const path = this.urlSerializer.serialize(this.rawUrlTree); @@ -722,12 +717,14 @@ export class Router { () => { if (navigationIsSuccessful) { this.navigated = true; - this.routerEvents.next(new NavigationEnd( - id, this.serializeUrl(url), this.serializeUrl(this.currentUrlTree))); + (this.events as Subject) + .next(new NavigationEnd( + id, this.serializeUrl(url), this.serializeUrl(this.currentUrlTree))); resolvePromise(true); } else { this.resetUrlToCurrentUrlTree(); - this.routerEvents.next(new NavigationCancel(id, this.serializeUrl(url), '')); + (this.events as Subject) + .next(new NavigationCancel(id, this.serializeUrl(url), '')); resolvePromise(false); } }, @@ -735,11 +732,12 @@ export class Router { if (isNavigationCancelingError(e)) { this.resetUrlToCurrentUrlTree(); this.navigated = true; - this.routerEvents.next( - new NavigationCancel(id, this.serializeUrl(url), e.message)); + (this.events as Subject) + .next(new NavigationCancel(id, this.serializeUrl(url), e.message)); resolvePromise(false); } else { - this.routerEvents.next(new NavigationError(id, this.serializeUrl(url), e)); + (this.events as Subject) + .next(new NavigationError(id, this.serializeUrl(url), e)); try { resolvePromise(this.errorHandler(e)); } catch (ee) { @@ -747,7 +745,7 @@ export class Router { } } - this.currentRouterState = storedState; + (this as{routerState: RouterState}).routerState = storedState; this.currentUrlTree = storedUrl; this.rawUrlTree = this.urlHandlingStrategy.merge(this.currentUrlTree, rawUrl); this.location.replaceState(this.serializeUrl(this.rawUrlTree)); @@ -936,7 +934,7 @@ function advanceActivatedRouteNodeAndItsChildren(node: TreeNode) function parentLoadedConfig(snapshot: ActivatedRouteSnapshot): LoadedRouterConfig|null { for (let s = snapshot.parent; s; s = s.parent) { - const route = s._routeConfig; + const route = s.routeConfig; if (route && route._loadedConfig) return route._loadedConfig; if (route && route.component) return null; } diff --git a/packages/router/src/router_state.ts b/packages/router/src/router_state.ts index 8008d68e1a..b017d59f3a 100644 --- a/packages/router/src/router_state.ts +++ b/packages/router/src/router_state.ts @@ -233,8 +233,8 @@ export function inheritedParamsDataResolve(route: ActivatedRouteSnapshot): Inher * @stable */ export class ActivatedRouteSnapshot { - /** @internal **/ - _routeConfig: Route|null; + /** The configuration used to match this route **/ + public readonly routeConfig: Route|null; /** @internal **/ _urlSegment: UrlSegmentGroup; /** @internal */ @@ -267,15 +267,12 @@ export class ActivatedRouteSnapshot { /** The component of the route */ public component: Type|string|null, routeConfig: Route|null, urlSegment: UrlSegmentGroup, lastPathIndex: number, resolve: ResolveData) { - this._routeConfig = routeConfig; + this.routeConfig = routeConfig; this._urlSegment = urlSegment; this._lastPathIndex = lastPathIndex; this._resolve = resolve; } - /** The configuration used to match this route */ - get routeConfig(): Route|null { return this._routeConfig; } - /** The root of the router state */ get root(): ActivatedRouteSnapshot { return this._routerState.root; } @@ -307,7 +304,7 @@ export class ActivatedRouteSnapshot { toString(): string { const url = this.url.map(segment => segment.toString()).join('/'); - const matched = this._routeConfig ? this._routeConfig.path : ''; + const matched = this.routeConfig ? this.routeConfig.path : ''; return `Route(url:'${url}', path:'${matched}')`; } } @@ -400,4 +397,4 @@ export function equalParamsAndUrlSegments( return equalUrlParams && !parentsMismatch && (!a.parent || equalParamsAndUrlSegments(a.parent, b.parent !)); -} \ No newline at end of file +}