Revert "fix(router): ensure routerLinkActive updates when associated routerLinks change (#38349)" (#38511)
This reverts commit e0e5c9f195460c7626c30248e7a79de9a2ebe377. Failures in Google tests were detected. PR Close #38511
This commit is contained in:
parent
723a9ff095
commit
bee44b3359
5
goldens/public-api/router/router.d.ts
vendored
5
goldens/public-api/router/router.d.ts
vendored
@ -378,7 +378,7 @@ export declare class RouterEvent {
|
|||||||
url: string);
|
url: string);
|
||||||
}
|
}
|
||||||
|
|
||||||
export declare class RouterLink implements OnChanges {
|
export declare class RouterLink {
|
||||||
fragment: string;
|
fragment: string;
|
||||||
preserveFragment: boolean;
|
preserveFragment: boolean;
|
||||||
/** @deprecated */ set preserveQueryParams(value: boolean);
|
/** @deprecated */ set preserveQueryParams(value: boolean);
|
||||||
@ -394,7 +394,6 @@ export declare class RouterLink implements OnChanges {
|
|||||||
};
|
};
|
||||||
get urlTree(): UrlTree;
|
get urlTree(): UrlTree;
|
||||||
constructor(router: Router, route: ActivatedRoute, tabIndex: string, renderer: Renderer2, el: ElementRef);
|
constructor(router: Router, route: ActivatedRoute, tabIndex: string, renderer: Renderer2, el: ElementRef);
|
||||||
ngOnChanges(changes: SimpleChanges): void;
|
|
||||||
onClick(): boolean;
|
onClick(): boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -430,7 +429,7 @@ export declare class RouterLinkWithHref implements OnChanges, OnDestroy {
|
|||||||
target: string;
|
target: string;
|
||||||
get urlTree(): UrlTree;
|
get urlTree(): UrlTree;
|
||||||
constructor(router: Router, route: ActivatedRoute, locationStrategy: LocationStrategy);
|
constructor(router: Router, route: ActivatedRoute, locationStrategy: LocationStrategy);
|
||||||
ngOnChanges(changes: SimpleChanges): any;
|
ngOnChanges(changes: {}): any;
|
||||||
ngOnDestroy(): any;
|
ngOnDestroy(): any;
|
||||||
onClick(button: number, ctrlKey: boolean, metaKey: boolean, shiftKey: boolean): boolean;
|
onClick(button: number, ctrlKey: boolean, metaKey: boolean, shiftKey: boolean): boolean;
|
||||||
}
|
}
|
||||||
|
@ -7,8 +7,8 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import {LocationStrategy} from '@angular/common';
|
import {LocationStrategy} from '@angular/common';
|
||||||
import {Attribute, Directive, ElementRef, HostBinding, HostListener, Input, isDevMode, OnChanges, OnDestroy, Renderer2, SimpleChanges} from '@angular/core';
|
import {Attribute, Directive, ElementRef, HostBinding, HostListener, Input, isDevMode, OnChanges, OnDestroy, Renderer2} from '@angular/core';
|
||||||
import {Subject, Subscription} from 'rxjs';
|
import {Subscription} from 'rxjs';
|
||||||
|
|
||||||
import {QueryParamsHandling} from '../config';
|
import {QueryParamsHandling} from '../config';
|
||||||
import {Event, NavigationEnd} from '../events';
|
import {Event, NavigationEnd} from '../events';
|
||||||
@ -115,7 +115,7 @@ import {UrlTree} from '../url_tree';
|
|||||||
* @publicApi
|
* @publicApi
|
||||||
*/
|
*/
|
||||||
@Directive({selector: ':not(a):not(area)[routerLink]'})
|
@Directive({selector: ':not(a):not(area)[routerLink]'})
|
||||||
export class RouterLink implements OnChanges {
|
export class RouterLink {
|
||||||
/**
|
/**
|
||||||
* Passed to {@link Router#createUrlTree Router#createUrlTree} as part of the `NavigationExtras`.
|
* Passed to {@link Router#createUrlTree Router#createUrlTree} as part of the `NavigationExtras`.
|
||||||
* @see {@link NavigationExtras#queryParams NavigationExtras#queryParams}
|
* @see {@link NavigationExtras#queryParams NavigationExtras#queryParams}
|
||||||
@ -167,9 +167,6 @@ export class RouterLink implements OnChanges {
|
|||||||
private commands: any[] = [];
|
private commands: any[] = [];
|
||||||
private preserve!: boolean;
|
private preserve!: boolean;
|
||||||
|
|
||||||
/** @internal */
|
|
||||||
onChanges = new Subject<void>();
|
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private router: Router, private route: ActivatedRoute,
|
private router: Router, private route: ActivatedRoute,
|
||||||
@Attribute('tabindex') tabIndex: string, renderer: Renderer2, el: ElementRef) {
|
@Attribute('tabindex') tabIndex: string, renderer: Renderer2, el: ElementRef) {
|
||||||
@ -178,13 +175,6 @@ export class RouterLink implements OnChanges {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @nodoc */
|
|
||||||
ngOnChanges(changes: SimpleChanges) {
|
|
||||||
// This is subscribed to by `RouterLinkActive` so that it knows to update when there are changes
|
|
||||||
// to the RouterLinks it's tracking.
|
|
||||||
this.onChanges.next();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Commands to pass to {@link Router#createUrlTree Router#createUrlTree}.
|
* Commands to pass to {@link Router#createUrlTree Router#createUrlTree}.
|
||||||
* - **array**: commands to pass to {@link Router#createUrlTree Router#createUrlTree}.
|
* - **array**: commands to pass to {@link Router#createUrlTree Router#createUrlTree}.
|
||||||
@ -308,9 +298,6 @@ export class RouterLinkWithHref implements OnChanges, OnDestroy {
|
|||||||
// TODO(issue/24571): remove '!'.
|
// TODO(issue/24571): remove '!'.
|
||||||
@HostBinding() href!: string;
|
@HostBinding() href!: string;
|
||||||
|
|
||||||
/** @internal */
|
|
||||||
onChanges = new Subject<void>();
|
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private router: Router, private route: ActivatedRoute,
|
private router: Router, private route: ActivatedRoute,
|
||||||
private locationStrategy: LocationStrategy) {
|
private locationStrategy: LocationStrategy) {
|
||||||
@ -349,9 +336,8 @@ export class RouterLinkWithHref implements OnChanges, OnDestroy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** @nodoc */
|
/** @nodoc */
|
||||||
ngOnChanges(changes: SimpleChanges): any {
|
ngOnChanges(changes: {}): any {
|
||||||
this.updateTargetUrlAndHref();
|
this.updateTargetUrlAndHref();
|
||||||
this.onChanges.next();
|
|
||||||
}
|
}
|
||||||
/** @nodoc */
|
/** @nodoc */
|
||||||
ngOnDestroy(): any {
|
ngOnDestroy(): any {
|
||||||
|
@ -7,8 +7,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import {AfterContentInit, ChangeDetectorRef, ContentChildren, Directive, ElementRef, Input, OnChanges, OnDestroy, Optional, QueryList, Renderer2, SimpleChanges} from '@angular/core';
|
import {AfterContentInit, ChangeDetectorRef, ContentChildren, Directive, ElementRef, Input, OnChanges, OnDestroy, Optional, QueryList, Renderer2, SimpleChanges} from '@angular/core';
|
||||||
import {from, of, Subscription} from 'rxjs';
|
import {Subscription} from 'rxjs';
|
||||||
import {mergeAll} from 'rxjs/operators';
|
|
||||||
|
|
||||||
import {Event, NavigationEnd} from '../events';
|
import {Event, NavigationEnd} from '../events';
|
||||||
import {Router} from '../router';
|
import {Router} from '../router';
|
||||||
@ -80,13 +79,14 @@ import {RouterLink, RouterLinkWithHref} from './router_link';
|
|||||||
exportAs: 'routerLinkActive',
|
exportAs: 'routerLinkActive',
|
||||||
})
|
})
|
||||||
export class RouterLinkActive implements OnChanges, OnDestroy, AfterContentInit {
|
export class RouterLinkActive implements OnChanges, OnDestroy, AfterContentInit {
|
||||||
|
// TODO(issue/24571): remove '!'.
|
||||||
@ContentChildren(RouterLink, {descendants: true}) links!: QueryList<RouterLink>;
|
@ContentChildren(RouterLink, {descendants: true}) links!: QueryList<RouterLink>;
|
||||||
|
// TODO(issue/24571): remove '!'.
|
||||||
@ContentChildren(RouterLinkWithHref, {descendants: true})
|
@ContentChildren(RouterLinkWithHref, {descendants: true})
|
||||||
linksWithHrefs!: QueryList<RouterLinkWithHref>;
|
linksWithHrefs!: QueryList<RouterLinkWithHref>;
|
||||||
|
|
||||||
private classes: string[] = [];
|
private classes: string[] = [];
|
||||||
private routerEventsSubscription: Subscription;
|
private subscription: Subscription;
|
||||||
private linkInputChangesSubscription?: Subscription;
|
|
||||||
public readonly isActive: boolean = false;
|
public readonly isActive: boolean = false;
|
||||||
|
|
||||||
@Input() routerLinkActiveOptions: {exact: boolean} = {exact: false};
|
@Input() routerLinkActiveOptions: {exact: boolean} = {exact: false};
|
||||||
@ -95,7 +95,7 @@ export class RouterLinkActive implements OnChanges, OnDestroy, AfterContentInit
|
|||||||
private router: Router, private element: ElementRef, private renderer: Renderer2,
|
private router: Router, private element: ElementRef, private renderer: Renderer2,
|
||||||
private readonly cdr: ChangeDetectorRef, @Optional() private link?: RouterLink,
|
private readonly cdr: ChangeDetectorRef, @Optional() private link?: RouterLink,
|
||||||
@Optional() private linkWithHref?: RouterLinkWithHref) {
|
@Optional() private linkWithHref?: RouterLinkWithHref) {
|
||||||
this.routerEventsSubscription = router.events.subscribe((s: Event) => {
|
this.subscription = router.events.subscribe((s: Event) => {
|
||||||
if (s instanceof NavigationEnd) {
|
if (s instanceof NavigationEnd) {
|
||||||
this.update();
|
this.update();
|
||||||
}
|
}
|
||||||
@ -104,23 +104,9 @@ export class RouterLinkActive implements OnChanges, OnDestroy, AfterContentInit
|
|||||||
|
|
||||||
/** @nodoc */
|
/** @nodoc */
|
||||||
ngAfterContentInit(): void {
|
ngAfterContentInit(): void {
|
||||||
// `of(null)` is used to force subscribe body to execute once immediately (like `startWith`).
|
this.links.changes.subscribe(_ => this.update());
|
||||||
from([this.links.changes, this.linksWithHrefs.changes, of(null)])
|
this.linksWithHrefs.changes.subscribe(_ => this.update());
|
||||||
.pipe(mergeAll())
|
this.update();
|
||||||
.subscribe(_ => {
|
|
||||||
this.update();
|
|
||||||
this.subscribeToEachLinkOnChanges();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private subscribeToEachLinkOnChanges() {
|
|
||||||
this.linkInputChangesSubscription?.unsubscribe();
|
|
||||||
const allLinkChanges =
|
|
||||||
[...this.links.toArray(), ...this.linksWithHrefs.toArray(), this.link, this.linkWithHref]
|
|
||||||
.filter((link): link is RouterLink|RouterLinkWithHref => !!link)
|
|
||||||
.map(link => link.onChanges);
|
|
||||||
this.linkInputChangesSubscription =
|
|
||||||
from(allLinkChanges).pipe(mergeAll()).subscribe(() => this.update());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Input()
|
@Input()
|
||||||
@ -135,8 +121,7 @@ export class RouterLinkActive implements OnChanges, OnDestroy, AfterContentInit
|
|||||||
}
|
}
|
||||||
/** @nodoc */
|
/** @nodoc */
|
||||||
ngOnDestroy(): void {
|
ngOnDestroy(): void {
|
||||||
this.routerEventsSubscription.unsubscribe();
|
this.subscription.unsubscribe();
|
||||||
this.linkInputChangesSubscription?.unsubscribe();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private update(): void {
|
private update(): void {
|
||||||
|
@ -14,54 +14,6 @@ import {RouterTestingModule} from '@angular/router/testing';
|
|||||||
|
|
||||||
describe('Integration', () => {
|
describe('Integration', () => {
|
||||||
describe('routerLinkActive', () => {
|
describe('routerLinkActive', () => {
|
||||||
it('should update when the associated routerLinks change - #18469', fakeAsync(() => {
|
|
||||||
@Component({
|
|
||||||
template: `
|
|
||||||
<a id="first-link" [routerLink]="[firstLink]" routerLinkActive="active">{{firstLink}}</a>
|
|
||||||
<div id="second-link" routerLinkActive="active">
|
|
||||||
<a [routerLink]="[secondLink]">{{secondLink}}</a>
|
|
||||||
</div>
|
|
||||||
`,
|
|
||||||
})
|
|
||||||
class LinkComponent {
|
|
||||||
firstLink = 'link-a';
|
|
||||||
secondLink = 'link-b';
|
|
||||||
|
|
||||||
changeLinks(): void {
|
|
||||||
const temp = this.secondLink;
|
|
||||||
this.secondLink = this.firstLink;
|
|
||||||
this.firstLink = temp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Component({template: 'simple'})
|
|
||||||
class SimpleCmp {
|
|
||||||
}
|
|
||||||
|
|
||||||
TestBed.configureTestingModule({
|
|
||||||
imports: [RouterTestingModule.withRoutes(
|
|
||||||
[{path: 'link-a', component: SimpleCmp}, {path: 'link-b', component: SimpleCmp}])],
|
|
||||||
declarations: [LinkComponent, SimpleCmp]
|
|
||||||
});
|
|
||||||
|
|
||||||
const router: Router = TestBed.inject(Router);
|
|
||||||
const fixture = createRoot(router, LinkComponent);
|
|
||||||
const firstLink = fixture.debugElement.query(p => p.nativeElement.id === 'first-link');
|
|
||||||
const secondLink = fixture.debugElement.query(p => p.nativeElement.id === 'second-link');
|
|
||||||
router.navigateByUrl('/link-a');
|
|
||||||
advance(fixture);
|
|
||||||
|
|
||||||
expect(firstLink.nativeElement.classList).toContain('active');
|
|
||||||
expect(secondLink.nativeElement.classList).not.toContain('active');
|
|
||||||
|
|
||||||
fixture.componentInstance.changeLinks();
|
|
||||||
fixture.detectChanges();
|
|
||||||
advance(fixture);
|
|
||||||
|
|
||||||
expect(firstLink.nativeElement.classList).not.toContain('active');
|
|
||||||
expect(secondLink.nativeElement.classList).toContain('active');
|
|
||||||
}));
|
|
||||||
|
|
||||||
it('should not cause infinite loops in the change detection - #15825', fakeAsync(() => {
|
it('should not cause infinite loops in the change detection - #15825', fakeAsync(() => {
|
||||||
@Component({selector: 'simple', template: 'simple'})
|
@Component({selector: 'simple', template: 'simple'})
|
||||||
class SimpleCmp {
|
class SimpleCmp {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user