2016-12-20 43 views
1

重述我剛纔的問題,我有這條路,給了我這樣的事情/order-screen/store/1/LondonAngular2網址參數返回NaN的

{ 
    path: 'order-screen/store/:storeId/:storeLocation', 
    component: OrderComponent 
    } 

當我嘗試使用storeLocationthis.route.params它返回的NaN錯誤來獲得。我想要得到的位置London,不知道如果.params是正確的使用。

this.routeSub = this.route.params.subscribe(params => { 
    this.storeLocation = +params['storeLocation']; 
    console.log('Location >> ', this.storeLocation); 
}) 

回答

2

只需從參數之前刪除+。所以..

this.routeSub = this.route.params.subscribe(params => { 
this.storeLocation = params['storeLocation']; 
    console.log('Location >> ', this.storeLocation); 
}) 

+將一個字符串轉換爲一個號碼,因爲「倫敦」是不是一個數字,你得到了NaN的錯誤

+0

呀想通早些時候,不知何故我對我自己的問題沒意見」 t後。但我會給你的答案檢查:) – MrNew

+0

很高興你沒有卡住那裏太久,並找出你自己的:) –