角度2,永远不会为实现OnActivate的组件调用routerOnActivate方法 [英] Angular 2, routerOnActivate method is never called for a component that implements OnActivate

查看:90
本文介绍了角度2,永远不会为实现OnActivate的组件调用routerOnActivate方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Angular 2 Router,使用官方文档中的示例代码记录当前路由:

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方法.在您的示例中,如果您在Home Component中实现它,它将被调用.

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,永远不会为实现OnActivate的组件调用routerOnActivate方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆