角2,routerOnActivate方法不会被调用为impements组件OnActivate [英] Angular 2, routerOnActivate method is never called for a component that impements OnActivate
问题描述
我试图登录使用角2路由器当前路由,使用从官方文档样本$ C $ C:的 https://angular.io/docs/ts/latest/api/router/OnActivate-interface.html
I am trying to log the current route using 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}"`;
}
}
该方法 routerOnActivate
永远不会被调用。
我配置使用 RouteConfig
标注的路线:
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 something else that I should configure in the Router to activate listeners ?
推荐答案
您需要实现解决路径组件内的routerOnActivate方法。在你的榜样,它会被调用,如果你实现它在家庭的组成部分。
You need to implement the routerOnActivate method inside a component that resolves the path. In your example it'll be called if you implement it in the Home Component.
这篇关于角2,routerOnActivate方法不会被调用为impements组件OnActivate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!