wanjingy.github.io / hw8 / frontend / src / app / app.component.ts
app.component.ts
Raw
import { Component } from '@angular/core';
import { Router, NavigationEnd } from '@angular/router';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  navs: string[] = ['Search', 'Favorites'];
  selectedIndex: number = 0;
  setIndex(index: number) {
    this.selectedIndex = index;
  }
  constructor(private router: Router) {
    router.events.subscribe(event => {
      if (event instanceof NavigationEnd) {
        if (event.url == '/search') {
          this.selectedIndex = 0;
        } else if (event.url == '/favorites') {
          this.selectedIndex = 1;
        }
      };
    });
  }
}