1

EDIT: (solution)

The root-cause of my problem was, that there had been additional routes added programmatically within the 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. 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?

4
  • have you try /:itemId? Commented Jan 11, 2019 at 14:42
  • Does that mean that you want associate ItemDetailsComponent with the route url of your app ? Commented Jan 11, 2019 at 14:43
  • @Mehdi yes, exactly Commented Jan 11, 2019 at 14:44
  • @Abhishek I tried that, but angular complains about routes not beginning with a slash Commented Jan 11, 2019 at 14:44

3 Answers 3

2

Looks like you're still trying to navigate to details/:itemId when you should have done it for just :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. But /details/1 or any such path would result in an error.

Here's a Working Sample StackBlitz for your ref.

Sign up to request clarification or add additional context in comments.

7 Comments

your stackblitz works! But it does not work within my applocation... I put in the route by hand
@TobiasGassmann, what route did you put? Please tell me the exact path that you're trying to navigate your user to.
www.example.com/123
and I cannot get my hands on the "123"
Can you please fork my StackBlitz link and make changes as per your scenario so that you can replicate this issue?
|
1

It does work fine. See the stackblitz: https://stackblitz.com/edit/angular-nqzpum

3 Comments

Create a stackblitz please
It seems I cannot reproduce my local problem in a stackblitz.. in stackblitz it works fine.. I will keep trying
If the route configuration is inside some module other than the AppModule, then you may need to subscribe to the parent's params using this.activatedRoute.parent.params.subscribe
1

in this case you don't really need to specify the variable in your router.module which should look like this :

{
  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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.