加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Angular 2,从不为实现OnActivate的组件调用routerOnActivate方法

发布时间:2020-12-17 07:17:31 所属栏目:安全 来源:网络整理
导读:我正在尝试使用Angular 2 Router记录当前路由,使用官方文档中的示例代码: https://angular.io/docs/ts/latest/api/router/OnActivate-interface.html import {Component} from 'angular2/core';import { OnActivate,ComponentInstruction} from 'angular2/r
我正在尝试使用Angular 2 Router记录当前路由,使用官方文档中的示例代码: 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',name: 'Home',data: {title: 'Welcome Home'}},{path: '/**',redirectTo: ['Index']}
])

我应该在路由器中配置其他东西来激活监听器吗?

您需要在解析路径的组件中实现routerOnActivate方法.在您的示例中,如果您在Home Component中实现它,它将被调用.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读