【发布时间】:2020-01-24 06:22:38
【问题描述】:
我正在使用 Angular 8 和 Django 3。我不明白我的 url 路径在 Angular 中的构建方式。我有一个restaurantlist 组件和一个restaurantdetail 组件,在我的app-routing-module.ts 中有以下路径:
const routes: Routes = [
{path:'restaurantlist', component:RestaurantlistComponent},
{path:'restaurantdetail/:id', component:RestaurantViewComponent}
];
在我的restaurantlist.component.html 文件中:
<h2>List of Restaurants</h2>
<ul *ngFor = "let restaurant of restaurants">
<a [routerLink]="['restaurantdetail', restaurant.id]">{{restaurant.name}}</a>
</ul>
当我点击我的restaurant.component.html 文件中的一个链接时,它尝试发送给我的链接是localhost:4200\restaurantlist\restaurantdetail\2。我不想要这个,我希望它把我发送到localhost:4200\restaurantdetail\2,因为这就是我设置 django url 的方式。为什么它继承了 url 的 restaurantlist 部分(好像它是这个组件的子视图或其他东西),我该如何摆脱它?这是否与我如何定义指向restaurantlist 组件的链接有关:
<a [routerLink]="'/restaurantlist'">See Available Restaurants</a>
感谢任何输入。
【问题讨论】:
-
尝试 /restaurantDetail 使用绝对路线