簡體   English   中英

為什么在我的angular2應用程序中檢索參數數據不起作用?

[英]Why does it not work to retrieve parameter data in my angular2 app?

我想將數據作為參數發送,並在用打字稿編寫的angular2應用程序的其他組件中檢索該數據。 在發送數字類型的數據時,我可以成功完成此操作,但是在發送字符串數據時,在不同情況下,它會失敗。 在這種情況下,檢索到的數據為NaN。 數據在URL中可見。 我查看了angular2文檔以及其他stackoverflow問題,但找不到解決方案。 這些是相關的代碼片段。

app.routes.ts

  { path: 'recommendations', component: Recommendations,
    children:[
      {path:'info/:mod-title/:mod-desc', component:ModalInfo},
      {path:'map/:lat/:lng', component:Map}

    ]},

Recommendation.component.ts

  loadInfo(){
    this.router.navigate(['/recommendations/info/:'+this.modTitle+'/:'+this.modDesc]); 

    loadMap(){
      this.router.navigate(['/recommendations/map/:'+this.lat+'/:'+this.lng]); 
    }

modal-info.component.ts

  ngOnInit():any {
    this.sub = this.modalRoute
      .params
      .subscribe(params => {
        this.itemTitle = +params['mod-title'];
        this.itemDesc = +params['mod-desc'];
      });
    alert(this.itemTitle+" "+this.itemDesc);
    this.loadData();
  } //This alerts NaN

google.component.ts

  ngOnInit():any {
    this.sub = this.route
      .params
      .subscribe(params => {
        this.lat = +params['lat'];
        this.lng = +params['lng'];
      });
  } //This assigns the data correctly.

您是否意識到+運算符正在嘗試將字符串轉換為數字? 因此,如果字符串不是數字,則可能會返回NaN

暫無
暫無

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

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