2016-07-08 22 views
0

在應用路線: 如何恢復從路徑角度RC4數據

{ path: 'calculator', component: CalculatorComponent, data: {physical: Physical}},

在設置comoponent:

if(this.selectedPhysical) { 
 
    this.router.navigate(['/calculator', this.selectedPhysical ]); 
 
}

導航的結果努力要做到這一點(這顯然是錯誤的):

http://localhost:3000/calculator;pK_Physical=4;fK_Measurement=2;dateString=5/22/2016;weight=270;height=70;hips=27;waist=51;neck=18.5

那麼,如何將數據傳遞到路線?

在計算器組件中,我有以下幾點。如何檢索傳入的路線數據?

ngOnInit() { 
 
    this.route 
 
     .data 
 
     .subscribe(v => this.selectedPhysical = <Physical>v); 
 
      
 
    console.log("phys = " + this.selectedPhysical); 
 
}

任何人都知道如何做到這一點?

+0

這將是巨大的,如果你不使用片斷功能的代碼是不可運行。你的大部分問題都由完全無用的鮮豔的按鈕組成。改爲使用「{}」按鈕。 –

+0

只需使用共享服務在組件之間進行通信即可。數據目前僅用於靜態添加到路由的數據,路由器提供的所有內容都反映在URL –

+0

好..我剛開始使用這個網站,因爲它是唯一獲得angular2幫助的地方..我會嘗試使用{ }按鈕,你想讓我重做這個帖子嗎?或者你會告訴我我在路由中做錯了什麼? –

回答

1

您需要將取決於該代碼更新的數據到subscribe(...)回調

ngOnInit() { 

    ** problem is here **, I've tried every combination of things and nothing works. Its always undefine. 

    this.route 
     .data 
     .subscribe(v => { 
      this.selectedPhysical = <Physical>v; 
      console.log("phys = " + this.selectedPhysical); 
     }); 

    // don't know about these   
    this.selectedSex = this.userSettings.sex; 

    //console.log('route id =' + this.route.snapshot.params.id) 
} 
+0

我已經完成了您的建議,並且在使用時仍未定義。還有其他建議嗎?謝謝。,。 –

+0

什麼是未定義的? –

+0

this.selectedPhysical。 –