简体   繁体   English

参数不起作用的角度路由

[英]Angular routing with parameter not working

I've installed the new SPA templates for .NET Core, and created a new Angular project. 我已经为.NET Core安装了新的SPA模板,并创建了一个新的Angular项目。 Here is a link if you're interested to give it a go. 如果您有兴趣试试这里有一个链接 It comes with an example of routing. 它附带一个路由示例。 Now I am trying to get routing to work with a parameter, but I am having difficulty getting it to work. 现在我正在尝试使用参数进行路由,但是我很难让它工作。

In app.module.ts, I am defining the path as follows (with a parameter): 在app.module.ts中,我按如下方式定义路径(带参数):

{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: 'home', component: HomeComponent },
{ path: 'counter', component: CounterComponent },
{ path: 'fetch-data', component: FetchDataComponent },
{ path: 'fetch-data:id', component: FetchDataComponent },
{ path: '**', redirectTo: 'home' }

And this is how I am defining the router link: 这就是我定义路由器链接的方式:

<a [routerLink]="['/fetch-data',3]">
    <span class='glyphicon glyphicon-th-list'></span> Fetch data by ID
</a>

The path shows correct when I hover over the link, but when I click on it, it takes me back to home. 当我将鼠标悬停在链接上时,路径显示正确,但是当我点击它时,它会将我带回家。 I thought maybe it is conflicting with the path above it so I deleted the following line, but still it doesn't work: 我想也许它与上面的路径相冲突所以我删除了以下行,但它仍然不起作用:

{ path: 'fetch-data', component: FetchDataComponent },

Change 更改

{ path: 'fetch-data:id', component: FetchDataComponent },

To

{ path: 'fetch-data/:id', component: FetchDataComponent },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM