2017-01-11 5 views
0

私のカートに今すぐ購入ボタンがあり、それをクリックすると、バックエンドのアイテムの詳細がプッシュされ、何かを返してからカートに自動的にナビゲートします。値を解決したらルータにナビゲートする

私の現在の実装を見てください。

addItem(){ 
    if(!localStorage.getItem('currentUserToken')){ 
     this._router.navigate(['/login']); 
    } else { 
     this.product[0].userId = localStorage.getItem('currentUserId'); 
     console.log(this.product); 
     this._productsService.addToCart(this.product) 
      .subscribe((updated) => console.log(updated), 
      err => console.log(err)); 
    } 
} 

buyNow(){ 
    this.addItem(); 
    this._router.navigate(['/cart']); 
} 

ここでの問題は、バックエンドがデータベースにプッシュしてからangle2に戻るよりも速くナビゲーションが行われることです。どのように解決が完了してからナビゲートするのを待つことができますか?あなたは以下のようにバックエンドからの応答を取得した後にリダイレクトする必要が

答えて

1

addItem(){ 
    if(!localStorage.getItem('currentUserToken')){ 
     this._router.navigate(['/login']); 
    } else { 
     this.product[0].userId = localStorage.getItem('currentUserId'); 
     console.log(this.product); 
     this._productsService.addToCart(this.product) 
      .subscribe((updated) => { 
       console.log(updated); 
       this._router.navigate(['/cart']); 
      }, 
      err => console.log(err)); 
    } 
} 

buyNow(){ 
    this.addItem();  
} 
+0

のaddItem()をカートに行かなくても、カートボタンに私のアドオンのために使用されています。今すぐ購入ボタンをクリックするとカートにアイテムが追加され、カートページにリダイレクトされます。私の論理を誤解して申し訳ありません。 –

関連する問題