2016-09-14 80 views
0

我想用可選的參數調用路線。無法匹配任何路線:使用查詢參數

我的路由器

const appRoutes: Routes = [ 

    { path: '', redirectTo: 'bo', pathMatch: 'full' }, 
    { path: 'login', component: LoginComponent }, 
    { path: 'bo', component: LayoutComponent, 
     children: [ 
      { path: '' , redirectTo: 'history', pathMatch: 'full'}, 
      { path: 'history', component: HistoryComponent } , 
      { path: 'history/details:id', component: DetailsComponent } 

     ] 
    },  
]; 

我該如何稱呼它

this.router.navigate(['./details', {id: myVarID}], { relativeTo: this.route }); 

錯誤:無法匹配任何路線: '波/歷史/詳細信息,ID = myIdValue'

但它的工作原理,如果我用斜線在路由器中定義它,並通過手動插入URL來調用它:

{ path: 'history/details/:id', component: DetailsComponent } 

即使我添加';'而不是路由器中的「/」,它不起作用。我想用router.navigate函數調用路由!

回答

0
{ path: 'history/details:id', component: DetailsComponent } 

它改變,

{ path: 'history/details/:id', component: DetailsComponent } //<----need to put router param after slash 

,並這樣稱呼它,

this.router.navigate(['/details', {id: myVarID}] 
+0

嘿!它仍然不起作用,錯誤是一樣的。我可以調用路由,如果我手動插入一個斜線,但功能導航使用';'調用它; –

0

好吧,這樣看來我犯了一個很大的混亂。

我想與查詢參數,但沒有斜線訪問​​的路線,從而解決辦法是:

路線:

{ path: 'history/details', component: DetailsComponent } 

電話:

this.router.navigate(['details', {id: myVarID}], { relativeTo: this.route }); 

/:id爲不在這裏,因爲查詢參數不能以斜槓開頭並且是可選的。創建的路徑將是/博/歷史/詳細信息,ID = myVarIDContents

而且細節組件內我能得到的查詢參數爲:

export class DetailsComponent implements OnInit { 

    constructor(public route: ActivatedRoute) { } 

    ngOnInit() { 
    console.log(this.route.params.value); 
    } 

} 
相關問題