首页 > 解决方案 > Angular 路由器发送 GET 请求而不是加载组件

问题描述

当我导航到 /exercises 页面时,我得到一个空白页面,显示 GetAllExercises 方法的结果,而不是 ExerciseListComponent。但是,如果我在 RouterModule 中将路径更改为“锻炼”,它将不再与控制器匹配并正确加载锻炼列表组件。为什么直接访问控制器而不是加载组件?

RouterModule.forRoot([
    { path: 'exercises', component: ExerciseListComponent, pathMatch: 'full' },
    { path: 'workout-plans', component: WorkoutPlanListComponent, pathMatch: 'full' },
    { path: 'workout-sessions', component: WorkoutSessionListComponent, pathMatch: 'full' },
])
[HttpGet]
[Route("exercises")]
public JsonResult GetAllExercises()
{
    var context = new WorkoutTrackerContext();
    var exercises = context.Exercises.ToList();
    return Json(exercises);
}

标签: .netangular

解决方案


我想我明白了。它与 . 无关RouterModule。甚至是Angular。这出戏的唯一演员是浏览器和后端。

我猜您的后端会将所有未知路由重定向到index.html引导 Angular 应用程序的路径 - 但/exercises它是已知路由。因此是 JSON,而不是组件 - 实际上是 JSON 而不是 Angular 应用程序及其对路由的解释。

'/'一种解决方案是使用比基本 href更具体的东西(不知道,/app对于 Angular,/service对于 API?)。


推荐阅读