gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 17:17:51 26 4
gpt4 key购买 nike

我正在尝试使用 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']}
])

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

最佳答案

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

关于Angular 2,永远不会为实现 OnActivate 的组件调用 routerOnActivate 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34700108/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com