簡體   English   中英

Angular 2在同一樹級別的兩個組件之間傳遞數據?

[英]Angular 2 pass data between two components from the same tree level?

給定兩個角2分量:

  • ListItemsComponent。
  • ShowItemComponent。

當用戶從ListItemComponent選擇一個項目時,必須通過接收選定的項目對象將其重定向到ShowItemComponent

知道兩個組件都存在於同一樹級別中。

將對象項從listItemsComponent傳遞到ShowItemComponent的最佳方法是什么?

您可以使用Router.navigate並在構造函數的ListItemComponent中Router.navigate提供具有項ID的其他組件的路徑,以聲明Route constructor( private _router: Router)

 this._router.navigate(['..../ShowItemComponent', {itemId:itemId}])

並在ShowItemComponent中放入ActivatedRoute

constructor(private _activateRoute: ActivatedRoute){

      this._activateRoute.params.subscribe(
          (param: any) => {
            let itemId = param['itemId'];
    });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM