我不理解Angular 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/collections。当按钮被点击时,URL会在collection.component.ts中以编程方式更改:
this.router.navigate(['/collections/', collection.name]);
我最终进入了 localhost:4200/collections/name - 一切都很好。现在我想通过编程方式返回到 /collections(在 collectioninfo.component.ts 中):
this.router.navigate(['/collections']);
但是那样做不起作用。URL 没有改变,我收到一个错误消息,说无法加载参数 - 因此显然再次加载了 /collections/name。虽然我认为这可能是路径问题,但像这样的事情也不起作用:
this.router.navigate(['../collections']);
附加信息:当我手动重新加载/collections页面时,我被重定向回首页,但我认为这是另一个问题,与此行为无关。
app.routing.ts:
const APP_ROUTES: Routes = [
...
{ path: 'collections', component: CollectionComponent },
{ path: 'collections/:id', component: CollectionInfo },
];
navigateByUrl
而不是navigate
吗?我不知道它是否能解决你的问题,但我在我的编程页面更改中使用它。 - Surrealthis.router.navigateByUrl('/collections');
。 - Surreal