简体   繁体   English

角路由参数不可访问

[英]angular routing parameter not accessible

EDIT: (solution) 编辑:(解决方案)

The root-cause of my problem was, that there had been additional routes added programmatically within the app.component. 我的问题的根本原因是,在app.component中以编程方式添加了其他路由。 I found this by disabling all routes in the app-routing.module and still being mysteriously able to navigate to the route in question. 我通过禁用app-routing.module中的所有路由并仍然能够神秘地导航到所讨论的路由来发现这一点。 After removing the programmatically added routes everything was working as expected. 删除以编程方式添加的路由后,一切都按预期工作。 Thanks for the help everyone! 感谢大家的帮助! :-) :-)

ORIGINAL (question): 原文 (问题):

I am using the following code within my component to subscribe to parameters of the route: 我在组件中使用以下代码来订阅路由参数:

this.activatedRoute.params.subscribe((values) => {
  console.log('these are my parameters: ', values);
});

Everything works fine, I get the parameters of the route without any problems. 一切正常,我可以毫无问题地获得路线的参数。

This is the working configuration of the route: 这是路由的工作配置:

{
  path: 'details/:itemId',
  component: ItemDetailsComponent,
},

Now to the problem: When I try to simplify my route (since I do not need any other routes) the following problem arises: After making the following simple change to the route-configuration, I cannot access the route-parameters anymore (they are empty). 现在解决问题:当我尝试简化路由时(由于不需要其他路由),出现以下问题:在对路由配置进行以下简单更改之后,我无法再访问路由参数(它们是空)。

This is the defective configuration of the route: 这是路由的有缺陷的配置:

{
  path: ':itemId',
  component: ItemDetailsComponent,
},

Does anybody know, why I cannot access the single route-parameter anymore after I shortend the route? 有人知道,为什么我在缩短路线后不能再访问单个路线参数?

Looks like you're still trying to navigate to details/:itemId when you should have done it for just :itemId . 看起来当您只应该为:itemId完成时,您仍在尝试导航至details/:itemId :itemId Make sure you're navigating to the proper path. 确保您导航到正确的路径。

After you've done the change, /details would work and would be considered as itemId . 完成更改后, /details将起作用,并将被视为itemId But /details/1 or any such path would result in an error. 但是/details/1或任何此类路径都将导致错误。

Here's a Working Sample StackBlitz for your ref. 这是供您参考的工作样本StackBlitz

It does work fine. 它确实工作正常。 See the stackblitz: https://stackblitz.com/edit/angular-nqzpum 参见stackblitz: https ://stackblitz.com/edit/angular-nqzpum

in this case you don't really need to specify the variable in your router.module which should look like this : 在这种情况下,您实际上不需要在router.module中指定如下所示的变量:

{
  path: '',
  component: ItemDetailsComponent,
},

Then if you need to open a item in particular then use the 然后,如果您需要打开特别的物品,请使用

this.router.navigate(["", {itemId: 123}]);

provided that router is injected as Router where is call is made. 假设路由器被注入为进行呼叫的路由器

Then you need to inject in your ItemDetailsComponent activatedRoute: ActivatedRoute 然后,您需要在ItemDetailsComponent中注入ActivatedRoute:ActivatedRoute

this.activatedRoute.params.subscribe(
  (params) => {
    //ItemId will be available here when url changes
    console.log(params.itemId);
  }
);

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

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