2016-07-08 25 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。 –