Files
itzmineydev/src/app/shared/navbar/navbar.component.ts
T

50 lines
1.4 KiB
TypeScript

import {Component, OnInit, ChangeDetectionStrategy} from '@angular/core';
import {NavigationEnd, Router, RouterLink} from '@angular/router';
import {ViewportService} from '../services/viewport.service';
import {filter} from 'rxjs/operators';
import {TranslatePipe, TranslateService} from '@ngx-translate/core';
@Component({
selector: 'app-navbar',
templateUrl: './navbar.component.html',
styleUrls: ['./navbar.component.css'],
changeDetection: ChangeDetectionStrategy.Eager,
imports: [
RouterLink,
TranslatePipe
]
})
export class NavbarComponent implements OnInit {
currentLang: string;
activeLink: string = '';
isNarrow: boolean = false;
constructor(
private router: Router,
private deviceService: ViewportService,
private translate: TranslateService
) {
this.isNarrow = this.deviceService.isNarrow;
this.currentLang = this.translate.currentLang || this.translate.getDefaultLang() || 'en';
}
switchLang(lang: string) {
this.translate.use(lang);
this.currentLang = lang;
}
ngOnInit(): void {
this.router.events.pipe(
filter(event => event instanceof NavigationEnd)
).subscribe(() => {
// Update activeLink based on current route
const currentRoute = this.router.url;
if (currentRoute.startsWith('/blog')) {
this.activeLink = '/blog';
} else {
this.activeLink = currentRoute;
}
});
}
}