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

馋奶兔 提交于 2019-12-09 17:42:35

问题


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}"`;
    }
}

The method routerOnActivate is 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 something else that I should configure in the Router to activate listeners ?


回答1:


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.



来源:https://stackoverflow.com/questions/34700108/angular-2-routeronactivate-method-is-never-called-for-a-component-that-implemen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!