按钮的功能会根据我放置逻辑的位置而不同。

huangapple go评论66阅读模式
英文:

Button does different things depending on where i've placed the logic

问题

以下是翻译好的部分:

"can anyone explain to me why the 'all-movies' route does a GET request to my API when i place it where it is right now, but when i place it on top of 'create' it does the redirect, as it is supposed to. Here is the code :"

"有人能解释一下为什么 'all-movies' 路由在我当前的位置时会执行 GET 请求到我的 API,但当我将它放在 'create' 上面时会执行重定向,正如它应该的那样。以下是代码:"

请注意,我已经尽量保留原文中的代码部分,只翻译了问题描述部分。

英文:

can anyone explain to me why the 'all-movies' route does a GET request to my API when i place it where it is right now, but when i place it on top of 'create' it does the redirect, as it is supposed to. Here is the code :

const routes: Routes = [
 
  {
    path: 'create',
    component: CreateComponent,
    canActivate: [MovieGuardService]
  },
  {
    path: ':id',
    resolve: {
      cres: MovieResolver,
    },
    component: MovieComponent,
  },
  {
    path: ':id/edit',
    resolve: {
      cres: MovieResolver,
    },
    component: EditComponent,
    canActivate: [MovieGuardService]
  },
  {
    path: 'all-movies',
    component: AllMoviesComponent
   },

];

答案1

得分: 1

Angular的Routes数组中的路由顺序很重要,因为路由器会按顺序将URL与定义的路由进行匹配。当输入URL时,路由器会从数组顶部开始尝试找到匹配项,并一直持续到找到匹配项或达到数组末尾。

在你的代码中,all-movies路由位于路由数组的底部。当你导航到/all-movies URL时,路由器会按顺序将URL与路由进行匹配。由于路由/:id中的:id参数是一个通用参数,可以匹配任何值,路由器将/all-movies视为有效的:id参数,并将请求定向到MovieComponent而不是AllMoviesComponent。

英文:

The order of routes in Angular's Routes array is important because the router matches the URL against the defined routes in a sequential manner. When a URL is entered, the router tries to find a match starting from the top of the array and continues until it finds a match or reaches the end of the array.

In your code, the all-movies route is placed at the bottom of the routes array. When you navigate to the /all-movies URL, the router matches the URL against the routes sequentially. Since the :id parameter in the route /:id is a generic parameter that can match any value, the router considers /all-movies as a valid :id parameter and directs the request to the MovieComponent instead of the AllMoviesComponent.

huangapple
  • 本文由 发表于 2023年8月5日 03:30:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76838711.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定