AngularM
AngularM

Reputation: 16618

In angular 2 how do you detect route changes

This is my current attempt using the old libraries.

I'm now using the latest libs, how would I update this:

import { Component,  } from '@angular/core';
import { Location } from '@angular/common';
import { Router } from '@angular/router';

@Component({
  moduleId: module.id,
  selector: 'HeaderComponent',
  templateUrl: 'header.component.html'
})

export class HeaderComponent{
  router : Router;

  constructor(router: Router, location: Location){   
      this.router = router;

      this.router.changes.subscribe((currentRoute) => {
          let currentRoute = this.location.path();          
      })
  }
}

This is my module:

export * from './header.component';

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';

import { HeaderComponent } from './header.component';

@NgModule({
  imports: [RouterModule, CommonModule],
  declarations: [HeaderComponent],
  exports: [HeaderComponent],
  providers: []
})

export class HeaderModule { }

Upvotes: 6

Views: 16280

Answers (1)

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657058

In the new router it's

this.router.events.subscribe(...)

See also Angular 2 router event listener

Upvotes: 15

Related Questions