1
1
import { AccordionComponent } from './accordion.component' ;
2
- import { Component , ViewEncapsulation , HostBinding , ViewChild , HostListener } from '@angular/core' ;
2
+ import { Component , ViewEncapsulation , HostBinding , ViewChild , HostListener , SimpleChanges , OnChanges } from '@angular/core' ;
3
3
import { AccordionBaseLinkComponent } from './accordion-base-link-component.class' ;
4
- import { RouterLinkActive , Router } from '@angular/router' ;
4
+ import { RouterLinkActive , Router , UrlTree , Params } from '@angular/router' ;
5
5
6
6
@Component ( {
7
7
selector : 'd-accordion-item-routerlink' ,
8
8
templateUrl : './accordion-item-routerlink.component.html' ,
9
9
encapsulation : ViewEncapsulation . None ,
10
10
} )
11
- export class AccordionItemRouterlinkComponent extends AccordionBaseLinkComponent {
12
- @ViewChild ( RouterLinkActive , { static : false } ) routerLinkActiveDirective : RouterLinkActive ;
11
+ export class AccordionItemRouterlinkComponent extends AccordionBaseLinkComponent implements OnChanges {
12
+ @ViewChild ( RouterLinkActive ) routerLinkActiveDirective : RouterLinkActive ;
13
13
@HostBinding ( 'class.devui-router-active' )
14
14
get routerLinkActived ( ) : boolean {
15
15
return ! ! ( this . routerLinkActiveDirective && this . routerLinkActiveDirective . isActive ) ;
16
16
}
17
+ private set urlTree ( urlTree : UrlTree ) {
18
+ if ( urlTree ) {
19
+ this . queryParams = urlTree . queryParams ;
20
+ this . fragment = urlTree . fragment ;
21
+ } else {
22
+ this . queryParams = undefined ;
23
+ this . fragment = undefined ;
24
+ }
25
+ }
26
+
27
+ public path : string ;
28
+ public queryParams : Params ;
29
+ public fragment : string ;
17
30
18
31
constructor ( protected accordion : AccordionComponent , private router : Router ) {
19
32
super ( accordion ) ;
@@ -29,4 +42,24 @@ export class AccordionItemRouterlinkComponent extends AccordionBaseLinkComponent
29
42
} ) ;
30
43
}
31
44
}
45
+
46
+ ngOnChanges ( changes : SimpleChanges ) : void {
47
+ if ( changes [ 'item' ] ) {
48
+ if ( this . link ) {
49
+ this . urlTree = this . router . parseUrl ( this . link ) ;
50
+ const handelPath = ( url : string ) => {
51
+ let path = url ;
52
+ const index = url . indexOf ( '?' ) ;
53
+ if ( index > - 1 ) {
54
+ path = url . slice ( 0 , index ) ;
55
+ }
56
+ return path ;
57
+ } ;
58
+ this . path = handelPath ( this . link ) ;
59
+ } else {
60
+ this . urlTree = undefined ;
61
+ this . path = undefined ;
62
+ }
63
+ }
64
+ }
32
65
}
0 commit comments