NativeScript按钮点击事件在导航后不会调用ngOnInit但是onTap会调用

时间:2016-12-05 23:27:06

标签: typescript nativescript

我目前正在编写一个TypeScript应用程序,其中按钮以编程方式创建并添加到GridLayout。

    let currentPage;
    let topFrame = frameModule.topmost();
    currentPage = topFrame.currentPage;

    currentPage.actionBarHidden = true;
    currentPage.backgroundImage = currentPage.ios ? "res://baby_large_file.jpg" : "res://baby_large_file";

    var onTap = function (eventData) {
        this.login();
    };

    var grid = new GridLayout();
    let feeding = new buttonModule.Button();
    feeding.text = "F";
    feeding.id = "feeding";
    feeding.on(buttonModule.Button.tapEvent, onTap, this);

    grid.addChild(feeding);

    grid.addRow(new ItemSpec(1, GridUnitType.star));
    grid.addColumn(new ItemSpec(1, GridUnitType.star));

    GridLayout.setRow(feeding, 0);// shareButton set to row 2
    GridLayout.setColumn(feeding, 0);// shareButton set to row 2

    currentPage.content = grid;

登录功能只是调用用户服务,如果应用程序有有效令牌,则会将其路由到新页面:

    login() {
    this.userService.login(this.user)
        .subscribe(
        () => this.router.navigate(["/locations"]),
        (error) => alert("Unfortunately we could not find your account.")
    );

}

永远不会在这个新页面中调用ngOnInit函数

export class Locations implements OnInit {
    constructor (private router: Router, private locationService: LocationService ) {}

    ngOnInit() {
        console.log('ngOnInit');
    }
}

非常感谢任何有关为什么不起作用的帮助

1 个答案:

答案 0 :(得分:0)

在Angular-2环境中,使用NG技术进行导航是一种更好的工作流程,而不是NativeScript和Angular-2的混合。 这就是说我建议使用NG2方式导航,例如使用 nsRouterLink ,并避免使用Button进行导航(可以像here一样使用Label) 如果您担心如何控制服务,那么您可以针对登录/登录表单等情况实施route guards

也就是说,这对于组件的创建非常有效。相反,使用NativeScript代码技术,您可以使用Angular指令创建模板,并在应用程序中随时重用它们。 示例herehere