NativeScript按钮点击事件不会在导航后调用ngOnInit,但onTap会做
问题描述:
我目前正在编写一个TypeScript应用程序,其中按钮以编程方式创建并添加到GridLayout。NativeScript按钮点击事件不会在导航后调用ngOnInit,但onTap会做
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');
}
}
与为什么这不起作用任何帮助将是非常赞赏
答
要在Angular-2环境中,使用NG技术进行导航的工作流程更好,而NativeScript和Angular-2混合使用则更好。 不过我会建议使用例如NG2方式与nsRouterLink导航和避免使用按钮导航(可以使用标签,而不是像here) 如果你担心如何控制服务,那么你可以实现route guards像案例登录/登录表单。
也就是说,这对创建组件也是非常有效的。相反,使用NativeScript代码技术,您可以使用Angular指令创建您的模板,并在您的应用程序中随时重用它们。 示例here和here