2017-06-23 12 views
1

まあ、私はこの質問をする前にいくつかの研究をしました。角度ルートのint配列を渡す

これは私のパス

{ path: 'cart/:productIds', component: CartComponent } 

である私はAppComponentに以下のようにカートをクリックしたときにproductIdsを通過したいです。これはできますか?私の質問があり、この

this.productIds = route.snapshot.params["productIds"]; 

ようcartcomponentにngOnitでこれらのproductIdsにアクセスしようとし

goToCart() { 
this.router.navigate(['/cart', this.productIds]);} 

アム、これは効率的な通過の道と二つの構成要素間のIntのアレイにアクセスしますか?

コンポーネント間の状態(正しい単語かどうかわからない、私の背景はASP.NET)をどうやって管理しますか?

答えて

1

serviceを使用して、1つのコンポーネントから別のコンポーネントにIDの配列を保存します。それをURLに渡すのは安全な方法ではありません。

import { Injectable } from '@angular/core'; 
@Injectable() 
export class AppMessageQueuService { 

    myIds: Array<Number>=[]; 
    constructor() { 

    } 
    saveIds(produIds:any){ 
     this.myIds = produIds; 
    } 
    retrieveIDs(){ 
     return this.myIds; 
    } 

} 

その後、あなたは両方のコンポーネントには、このサービスを噴射することができるなど、第一成分からそれを保存し、

this.appMsgService.saveIds(yourIds); 

が続いて第二の成分に取得し、このようなサービスを作成します。 、

this.appMsgService.retrieveIDs(yourIds); 

constructor(
    private appMsgService: AppMessageQueuService 
)