2017-06-02 38 views
0

我想將參數傳遞給這種格式的一個組件www.domain.com?param=value,但是Angular繼續發送像這樣的參數www.domain.com;param=value。爲什麼要替換?;傳遞params angular 2傳統方式

這是我的路線的conf:

const router: Routes = [ 
    {path: '', redirectTo: '/shops', pathMatch: 'full'}, 
    {path: 'shops', component: ShopComponent, canActivate: [AuthManager]}, 
    {path: 'shop/new', component: NewShopComponent, canActivate: [AuthManager]}, 
    {path: 'shop/new/:id', component: NewShopComponent, canActivate: [AuthManager]}, 
    {path: 'login', component: LoginComponent}, 
    {path: 'register', component: RegisterComponent}, 
    {path: '**', component: ShopComponent} 
]; 

,這我怎麼值傳遞

this._router.navigate(['shops', {id: id}]); 

如果我通過它像

this._router.navigate(['shops?id=id']); 

我得到了一個未定義的路線錯誤。

回答

0

角度提供了三種類型的參數:

  • 必需的參數
  • 可選參數
  • 查詢參數

外觀URL和導航的語法使用的參數是不同的取決於你想使用的參數類型。

如果你想使用「?」樣式,那麼你想要的查詢參數。

查詢參數:

  • 不包括在路由路徑配置。所以如果你想查詢參數,不要將:id添加到路徑中。

不是這個:

{path: 'shop/new/:id', component: NewShopComponent, canActivate: [AuthManager]}, 

而會將這個:

{path: 'shop/new', component: NewShopComponent, canActivate: [AuthManager]}, 

然後你導航到該路線是這樣的:

this.router.navigate(['/shops'], 
    { 
    queryParams: { id: id } 
    } 
); 

如果你想如何更多信息使用路由...檢查了這一點:https://app.pluralsight.com/library/courses/angular-routing/table-of-contents

+0

如果我使用這種方式 this.router.navigate([ '/商店'], { queryParams:{ID:ID}} ); 我的路線成爲/商店; id =「id」< - 這是錯誤的,我想這個結果:/ shops?id =「id」 – user3613976

+0

你可以提供一個plunker?使用queryParams應該導致你正在尋找的URL:/ shops?id =「id」。 – DeborahK