我正在尝试使用官方文档中的示例代码使用 Angular 2 路由器记录当前路由: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}"`;
}
}
方法routerOnActivate
永远不会被调用。
RouteConfig
我使用注释配置了路由:
@RouteConfig([
{path: '/', component: Home, name: 'Index', data: {title: 'Index page'}},
{path: '/home', component: Home, name: 'Home', data: {title: 'Welcome Home'}},
{path: '/**', redirectTo: ['Index']}
])
我应该在路由器中配置其他东西来激活侦听器吗?