Angular 角度4:路由到一个路由不';行不通

Angular 角度4:路由到一个路由不';行不通,angular,typescript,angular2-routing,angular4-router,Angular,Typescript,Angular2 Routing,Angular4 Router,在Angular 4中有一些基本的布线概念我不理解 index.html: <base href="/"> const APP_ROUTES: Routes = [ ... { path: 'collections', component: CollectionComponent }, { path: 'collections/:id', component: CollectionInfo }, ]; 我的header.component.html中有一个链接: &l

在Angular 4中有一些基本的布线概念我不理解

index.html

<base href="/">
const APP_ROUTES: Routes = [
  ...
  { path: 'collections', component: CollectionComponent },
  { path: 'collections/:id', component: CollectionInfo },
];
我的header.component.html中有一个链接:

<a class="nav-link" [routerLink]="['/collections']">
    Collections
</a>
我最终在localhost:4200/collections/name上运行-一切正常。现在我在编程上想回到/collections(在collectioninfo.component.ts中):

但这不起作用。url没有改变,我得到一个错误,说无法加载一个参数-所以很明显,/collections/name再次被加载。虽然这可能是一个路径问题,但像这样的事情也不起作用:

this.router.navigate(['../collections']);
其他信息:当我在/collections上手动重新加载页面时,我再次被转发到主页,但我认为这是另一个问题,与此行为无关

app.routing.ts

<base href="/">
const APP_ROUTES: Routes = [
  ...
  { path: 'collections', component: CollectionComponent },
  { path: 'collections/:id', component: CollectionInfo },
];

在您的相对路径
this.router.navigate(['../collections'])
您正试图导航到
localhost:4200/collections/collections
。尝试
this.router.navigate(['../'])

如果这不起作用,还应将ActivatedRoute作为relativeTo参数提供:

constructor(route: ActivatedRoute, router: Router) {}

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

relative to
通过创建与您提供的任何条目相关的路径来工作,它不一定是当前路径。

结果表明我的firebase代码出错了-它尝试在转到其他路径之前重新加载页面。这导致了一个错误,因为从上一条路线移交的一些参数丢失

export const routing = RouterModule.forRoot(APP_ROUTES, { enableTracing: true })

在app.routing.ts中进行调试非常有用。

您是否尝试了
navigateByUrl
而不是
navigate
,我不知道它是否能解决您的问题,但我将其用于编程页面更改感谢提示,不幸的是,这也不起作用(某些行为)。您的空路径是如何定义的?标题中的哪些内容已验证?翻转路由顺序,首先创建最具体的路由。您得到有关缺少参数的错误的原因是
/collections
正在通过
CollectionsInfo
路由。或者,对路线使用精确匹配。@超现实主义:它只是在没有括号的情况下工作。但是谢谢你的提示。this.router.navigate(['../'])不会抛出错误,但会将我引导到我的主页。但是,此.router.navigate(['../collections'])不起作用,它抛出与上述相同的错误。然后我尝试了ActivatedRoute:Imported它(import{ActivatedRoute}来自'@angular/router';)并将其放入构造函数中,但是“relativeTo:route”抛出语法错误:找不到任何名称“route”。尽管它正确地写在构造函数中:route:ActivatedRoute。你有预感为什么会这样吗?试着把
route
改成
this.route
。我犯这个错误的次数比我想承认的要多。这可能会很尴尬,但我不知道我在语法方面做错了什么。一定是件小事,但我没看见。由于它与原始版本无关,我认为最好将其发布在pastebin:。你能在那里发现任何明显的错误吗?我看不出你那里的东西有任何错误,并且使用这个.router.navigate(['/collections']);应该也很好。也许还有别的事。。但我看不出你的东西有什么问题。很抱歉,我再也帮不上忙了:(我在构造函数中漏掉了“public”。但是相对论并不能解决这个问题。不过谢谢你的帮助。
export const routing = RouterModule.forRoot(APP_ROUTES, { enableTracing: true })