在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