6

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 { }
AngularM
  • 15,982
  • 28
  • 94
  • 169

1 Answers1

15

In the new router it's

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

See also Angular 2 router event listener

Community
  • 1
  • 1
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • That seems to work fine. this.location.path() doesnt work with new libs either, any ideas? its in my above code and its complaining about path – AngularM Oct 30 '16 at 17:20
  • `this.location.path()` should work https://angular.io/docs/ts/latest/api/common/index/Location-class.html – Günter Zöchbauer Oct 30 '16 at 17:21