Created
August 10, 2018 17:26
-
-
Save wholypantalones/9733e931d812a07d14964fa1fe98c5cd to your computer and use it in GitHub Desktop.
Angular 6 routerLinkActive with params workaround
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {NgModule} from '@angular/core'; | |
import {RouterModule, Routes} from '@angular/router'; | |
import {DashboardViewRouteComponent} from '@routes/dashboard-view-route/dashboard-view-route.component'; | |
import {ProjectsViewRouteComponent} from '@routes/projects-view-route/projects-view-route.component'; | |
import {ProjectsViewDetailsRouteComponent} from '@routes/projects-view-details-route/projects-view-details-route.component'; | |
import {ProjectsAddViewRouteComponent} from '@routes/projects-add-view-route/projects-add-view-route.component'; | |
import {ProjectsViewDetailsTableComponent} from '@routes/projects-view-details-table/projects-view-details-table.component'; | |
const routes: Routes = [ | |
{path: '', redirectTo: 'dashboard', pathMatch: 'full'}, | |
{path: 'dashboard', component: DashboardViewRouteComponent}, | |
{path: 'projects', component: ProjectsViewRouteComponent}, | |
{ | |
path: 'projects/:configGroupId/:domainGroup', | |
component: ProjectsViewDetailsRouteComponent, | |
children: [{ | |
path: '', | |
component: ProjectsViewDetailsTableComponent | |
}, { | |
path: 'add', | |
component: ProjectsAddViewRouteComponent | |
}] | |
} | |
]; | |
@NgModule({ | |
exports: [RouterModule], | |
imports: [RouterModule.forRoot(routes, {useHash: true})], | |
}) | |
export class AppRoutingModule { | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="container-fluid detail-view"> | |
<nav mat-tab-nav-bar class="mb-4"> | |
<a mat-tab-link | |
*ngFor="let link of navLinks" | |
[routerLink]="link.path" | |
routerLinkActive #rla="routerLinkActive" | |
[routerLinkActiveOptions]="{exact: false}" | |
[active]="link.path === currentPath"> | |
{{link.label}} | |
</a> | |
</nav> | |
<router-outlet></router-outlet> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// compare the current url with the parent url | |
import {Component, OnInit} from '@angular/core'; | |
import {ActivatedRoute, Router, NavigationEnd} from '@angular/router'; | |
@Component({ | |
selector: 'app-projects-view-details-route', | |
templateUrl: './projects-view-details-route.component.html', | |
styleUrls: ['./projects-view-details-route.component.scss'] | |
}) | |
export class ProjectsViewDetailsRouteComponent implements OnInit { | |
path = '/' + this.route.snapshot.url.map(o => o.path).join('/'); | |
currentPath: any; | |
navLinks: any[] = []; | |
constructor( | |
private route: ActivatedRoute, | |
private router: Router | |
) { | |
this.router.events.subscribe((event) => { | |
if (event instanceof NavigationEnd) { | |
const url = event.url.split('?'); | |
this.currentPath = decodeURIComponent(url[0]); | |
} | |
}); | |
} | |
ngOnInit() { | |
this.navLinks = [{ | |
path: this.path, | |
label: 'Banners' | |
}, { | |
path: this.path + '/add', | |
label: 'Add New' | |
}]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment