2016-12-05 86 views
1

我目前正在編寫一個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'); 
    } 
} 

與爲什麼這不起作用任何幫助將是非常讚賞

回答

0

要在Angular-2環境中,使用NG技術進行導航的工作流程更好,而NativeScript和Angular-2混合使用則更好。 不過我會建議使用例如NG2方式與nsRouterLink導航和避免使用按鈕導航(可以使用標籤,而不是像here) 如果你擔心如何控制服務,那麼你可以實現route guards像案例登錄/登錄表單。

也就是說,這對創建組件也是非常有效的。相反,使用NativeScript代碼技術,您可以使用Angular指令創建您的模板,並在您的應用程序中隨時重用它們。 示例herehere

相關問題