简体   繁体   English

Angular 中的 URL 路径构建

[英]Url path construction in Angular

I am using Angular 8 and Django 3. I do not understand the way that my url path is being built in Angular.我正在使用 Angular 8 和 Django 3。我不明白在 Angular 中构建我的 url 路径的方式。 I have a restaurantlist component and a restaurantdetail component with the following paths in my app-routing-module.ts :我有一个restaurantlist组件和一个restaurantdetail组件,在我的app-routing-module.ts有以下路径:

const routes: Routes = [
  {path:'restaurantlist', component:RestaurantlistComponent},
  {path:'restaurantdetail/:id', component:RestaurantViewComponent}
];

In my restaurantlist.component.html file:在我的restaurantlist.component.html文件中:

<h2>List of Restaurants</h2>
<ul *ngFor = "let restaurant of restaurants">
<a [routerLink]="['restaurantdetail', restaurant.id]">{{restaurant.name}}</a>  
</ul>

When I go to click on one of the links in my restaurant.component.html file, the link that it tries to send me to is localhost:4200\\restaurantlist\\restaurantdetail\\2 .当我点击我的restaurant.component.html文件中的链接之一时,它试图发送给我的链接是localhost:4200\\restaurantlist\\restaurantdetail\\2 I dont want this, I want it to send me to localhost:4200\\restaurantdetail\\2 , since this is how I have my django urls set up.我不想要这个,我想让它把我发送到localhost:4200\\restaurantdetail\\2 ,因为这是我设置 django url 的方式。 Why does it inherit the restaurantlist part of the url (as if its a child view of this component or something) and how can I get rid of it?为什么它继承了 url 的restaurantlist部分(好像它是这个组件的子视图或其他东西),我该如何摆脱它? Does it have something to do with how I define my links for to the restaurantlist component:它是否与我如何定义到restaurantlist组件的链接有关:

<a [routerLink]="'/restaurantlist'">See Available Restaurants</a>

Any input is appreciated.任何输入表示赞赏。

A route without the / is a relative route, meaning you 'dive deeper' into a route.没有/的路线是相对路线,这意味着您“深入”路线。

localhost/main -> route to list -> localhost/main/list -> route to detail -> localhost/main/list/detail localhost/main -> 路由到list -> localhost/main/list -> 路由到detail -> localhost/main/list/detail

A route with the / is an absolute route meaning you want to go exactly there.带有/的路线是绝对路线,这意味着您想要准确地到达那里。

localhost/main -> route to /list -> localhost/list -> route to /detail -> localhost/detail. localhost/main -> 路由到/list -> localhost/list -> 路由到/detail -> localhost/detail。

If you are on the list page you can route to detail but if you are on the main page and want to go to detail you have to use /main/list/detail or list/detail .如果您在列表页面上,您可以路由到detail但如果您在主页上并想转到详细信息,则必须使用/main/list/detaillist/detail

尝试在路由前添加“/”

<a [routerLink]="['/restaurantdetail', restaurant.id]">{{restaurant.name}}</a>

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

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