2017-06-17 41 views
0

如何使用ID爲String的路由參數?使用ID爲String 4的路由參數是字符串

{path: 'param/:id', component: MyComponent}

let id = this.route.snapshot.params['id'];
任何想法

塊引用

+0

是什麼問題?你想從URL獲得ID?你想用ID導航? –

+0

問題不明確 –

+0

是的,我想用id導航。但是在角度類型的ID是數字,我想要的ID類型是STRING –

回答

1

使用路線快照來獲得你所顯示的查詢參數應該會給你一個字符串。如果你想該字符串隱式轉換爲數字,所以你可以在路由參數用於導航使用它,您可以前綴與+變量:

// (+) converts string 'id' to a number 
let id = +this.route.snapshot.params['id']; 

然後:

this.router.navigate(['/example', id]); 

退房在文檔 https://angular.io/guide/router#snapshot-the-no-observable-alternative中獲取查詢參數的示例。

+0

非常感謝,我會盡力。 –

+0

總是想知道+的意思。這其實是我的問題。之前跟隨他們使用+的英雄之旅。在這裏,我使用UUID,並且必須刪除所有+'es。謝謝。 – hogan

0

在您可以使用此代碼串變換成數字組件:

constructor(private route: ActivatedRoute) { } 

    ngOnInit() { 
    this.route.paramMap 
     .subscribe((params: ParamMap) => { 
     let id = +params.get('id'); // This line converts id from string into num 
     }); 
    } 
相關問題