Angular 2, the routerOnActivate method is never called for a component that implements OnActivate

I am trying to register the current route with Angular 2 Router using the sample code from the official documentation: https://angular.io/docs/ts/latest/api/router/OnActivate-interface.html

import {Component} from 'angular2/core'; import { OnActivate, ComponentInstruction } from 'angular2/router'; @Component({selector: 'header-title', template: `<div>routerOnActivate: {{log}}</div>`}) export class HeaderTitle implements OnActivate { log: string = ''; routerOnActivate(next: ComponentInstruction, prev: ComponentInstruction) { console.log('hello on activate'); this.log = `Finished navigating from "${prev ? prev.urlPath : 'null'}" to "${next.urlPath}"`; } } 

The routerOnActivate method routerOnActivate never called.

I configured the routes using the RouteConfig annotation:

 @RouteConfig([ {path: '/', component: Home, name: 'Index', data: {title: 'Index page'}}, {path: '/home', component: Home, name: 'Home', data: {title: 'Welcome Home'}}, {path: '/**', redirectTo: ['Index']} ]) 

Is there anything else I need to configure in Router to activate listeners?

+7
angular angular2-routing
source share
1 answer

You need to implement the routerOnActivate method inside the component that resolves the path. In your example, this will be called if you implement it in the home component.

+12
source share

All Articles