英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论