在Angular 4中有一些基本的路由概念,我不明白。角4:路由到路由不起作用
的index.html:
<base href="/">
文件結構:
- app
|- app.routings.ts
|- collections
|-- collection.component.ts
|-- collection.component.html
|-- collectioninfo.component.ts
|-- collectioninfo.component.html
|- shared
|-- header.component.html
|-- header.component.ts
我在header.component.html鏈接:如果我
<a class="nav-link" [routerLink]="['/collections']">
Collections
</a>
點擊它我登陸localhost:4200 /集。當點擊一個按鈕,該URL以編程方式改變(collection.component.ts):
this.router.navigate(['/collections/', collection.name]);
我結束了在localhost:4200 /收藏/名稱 - 無一不精。現在我編程地想要回到/ collections(在collectioninfo.component.ts中):
this.router.navigate(['/collections']);
但是,這並不起作用。 URL不會改變,我得到一個錯誤,說一個參數無法加載 - 顯然/集合/名稱再次加載。認爲這可能是一個路徑問題,但這樣的事情也不起作用:
this.router.navigate(['../collections']);
附加信息:當我手動刷新頁面,同時上/收藏我被轉發到主頁再次,但我認爲這是另一個問題,與此行爲無關。
app.routing.ts:
const APP_ROUTES: Routes = [
...
{ path: 'collections', component: CollectionComponent },
{ path: 'collections/:id', component: CollectionInfo },
];
您是否嘗試過'navigateByUrl'而不是'navigate',我不知道它是否會解決您的問題,但我使用它來編程頁面更改 – Surreal
感謝您的提示,不幸的是,這也不起作用一些行爲)。 – user3255061
您的空路徑是如何定義的以及在您的頭文件中是什麼? – JayDeeEss