2016-12-05 2 views
1

です。現在、ボタンがプログラムによって作成され、GridLayoutに追加されるTypeScriptアプリケーションを作成しています。NativeScript Button Tapイベントはナビゲーション後に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; 

ログイン機能は、単にユーザーサービスを呼び出し、アプリは、彼らが新しいページにルーティングされている有効なトークンを持っている場合:

ngOnInit機能は、この新しいページに呼び出されることはありません
login() { 
    this.userService.login(this.user) 
     .subscribe(
     () => this.router.navigate(["/locations"]), 
     (error) => alert("Unfortunately we could not find your account.") 
    ); 

} 

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

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

これが動作しない理由を持つすべてのヘルプは非常に

答えて

0

していただければ幸いAngular-2環境では、NativeScriptとAngular-2が混在しているのではなく、NG手法を使用してナビゲートする方が良いワークフローです。あなたがサービスを制御する方法を心配しているなら、あなたのような場合のためにroute guardsを実装することができ (代わりにhereのようなラベルを使用することができます)私はとnsRouterLink例えばNG2の方法を使用してナビゲートするお勧めしますとナビゲーション用のボタンを使用して避けるだろうと述べた サインイン/ログインフォーム。

これは、同じことがコンポーネントの作成にかなり有効だと言っています。代わりに、NativeScriptのコードテクニックを使用して、Angularディレクティブを使用してテンプレートを作成し、アプリケーションに必要なときにいつでも再利用することができます。 例hereおよびhere

関連する問題