From fcd1f6147614e9864f7dbfb98a474a07b0105087 Mon Sep 17 00:00:00 2001 From: Pawel Kozlowski Date: Wed, 6 Feb 2019 14:24:25 +0100 Subject: [PATCH] refactor(router): change RouterLinkActive impl to account for upcoming ivy breaking change (#28560) PR Close #28560 --- packages/router/src/directives/router_link_active.ts | 11 +++++++---- packages/router/test/integration.spec.ts | 1 - tools/public_api_guard/router/router.d.ts | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/router/src/directives/router_link_active.ts b/packages/router/src/directives/router_link_active.ts index 64596bec70..1b1338f705 100644 --- a/packages/router/src/directives/router_link_active.ts +++ b/packages/router/src/directives/router_link_active.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {AfterContentInit, ChangeDetectorRef, ContentChildren, Directive, ElementRef, Input, OnChanges, OnDestroy, QueryList, Renderer2, SimpleChanges} from '@angular/core'; +import {AfterContentInit, ContentChildren, Directive, ElementRef, Input, OnChanges, OnDestroy, Optional, QueryList, Renderer2, SimpleChanges} from '@angular/core'; import {Subscription} from 'rxjs'; import {NavigationEnd, RouterEvent} from '../events'; @@ -93,7 +93,8 @@ export class RouterLinkActive implements OnChanges, constructor( private router: Router, private element: ElementRef, private renderer: Renderer2, - private cdr: ChangeDetectorRef) { + @Optional() private link?: RouterLink, + @Optional() private linkWithHref?: RouterLinkWithHref) { this.subscription = router.events.subscribe((s: RouterEvent) => { if (s instanceof NavigationEnd) { this.update(); @@ -140,7 +141,9 @@ export class RouterLinkActive implements OnChanges, } private hasActiveLinks(): boolean { - return this.links.some(this.isLinkActive(this.router)) || - this.linksWithHrefs.some(this.isLinkActive(this.router)); + const isActiveCheckFn = this.isLinkActive(this.router); + return this.link && isActiveCheckFn(this.link) || + this.linkWithHref && isActiveCheckFn(this.linkWithHref) || + this.links.some(isActiveCheckFn) || this.linksWithHrefs.some(isActiveCheckFn); } } diff --git a/packages/router/test/integration.spec.ts b/packages/router/test/integration.spec.ts index df22c282c2..7b7d9fc022 100644 --- a/packages/router/test/integration.spec.ts +++ b/packages/router/test/integration.spec.ts @@ -3578,7 +3578,6 @@ describe('Integration', () => { TestBed.configureTestingModule({declarations: [RootCmpWithLink]}); const router: Router = TestBed.get(Router); - const loc: any = TestBed.get(Location); const f = TestBed.createComponent(RootCmpWithLink); advance(f); diff --git a/tools/public_api_guard/router/router.d.ts b/tools/public_api_guard/router/router.d.ts index 24231d1ea6..3e3098ddf7 100644 --- a/tools/public_api_guard/router/router.d.ts +++ b/tools/public_api_guard/router/router.d.ts @@ -390,7 +390,7 @@ export declare class RouterLinkActive implements OnChanges, OnDestroy, AfterCont routerLinkActiveOptions: { exact: boolean; }; - constructor(router: Router, element: ElementRef, renderer: Renderer2, cdr: ChangeDetectorRef); + constructor(router: Router, element: ElementRef, renderer: Renderer2, link?: RouterLink | undefined, linkWithHref?: RouterLinkWithHref | undefined); ngAfterContentInit(): void; ngOnChanges(changes: SimpleChanges): void; ngOnDestroy(): void;