feat(router): add isActive to router
This commit is contained in:
@ -10,7 +10,7 @@ import {AfterContentInit, ContentChildren, Directive, ElementRef, Input, OnChang
|
||||
import {Subscription} from 'rxjs/Subscription';
|
||||
|
||||
import {NavigationEnd, Router} from '../router';
|
||||
import {UrlTree, containsTree} from '../url_tree';
|
||||
import {UrlTree} from '../url_tree';
|
||||
|
||||
import {RouterLink, RouterLinkWithHref} from './router_link';
|
||||
|
||||
@ -97,18 +97,17 @@ export class RouterLinkActive implements OnChanges, OnDestroy, AfterContentInit
|
||||
private update(): void {
|
||||
if (!this.links || !this.linksWithHrefs || !this.router.navigated) return;
|
||||
|
||||
const currentUrlTree = this.router.parseUrl(this.router.url);
|
||||
const isActiveLinks = this.reduceList(currentUrlTree, this.links);
|
||||
const isActiveLinksWithHrefs = this.reduceList(currentUrlTree, this.linksWithHrefs);
|
||||
const isActiveLinks = this.reduceList(this.links);
|
||||
const isActiveLinksWithHrefs = this.reduceList(this.linksWithHrefs);
|
||||
this.classes.forEach(
|
||||
c => this.renderer.setElementClass(
|
||||
this.element.nativeElement, c, isActiveLinks || isActiveLinksWithHrefs));
|
||||
}
|
||||
|
||||
private reduceList(currentUrlTree: UrlTree, q: QueryList<any>): boolean {
|
||||
private reduceList(q: QueryList<any>): boolean {
|
||||
return q.reduce(
|
||||
(res: boolean, link: any) =>
|
||||
res || containsTree(currentUrlTree, link.urlTree, this.routerLinkActiveOptions.exact),
|
||||
res || this.router.isActive(link.urlTree, this.routerLinkActiveOptions.exact),
|
||||
false);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user