-2

I don't fully understand how to use the ActivatedRoute service. In my application the code to create or edit a user is exactly the same with the only difference being that an "edit" first queries for the current values from a webservice whereas the "create" does not.

What I'd like to have is a route like so:

{path: 'client/:id', component: ClientEditComponent}

and then if the :id is provided I'm doing an edit, and if not I'm doing a create. There's so many different ways that I've seen of reading from the ActivatedRoute service that I don't know what's right at this point.

Am I just looking at snapshot, do I subscribe to something, etc...

Gargoyle
  • 9,590
  • 16
  • 80
  • 145
  • Are you saying that if an `:id` is not provided you still want it to use the `ClientEditComponent`? – CTheCheese Jan 05 '19 at 22:20
  • Yes, that would be the goal. – Gargoyle Jan 05 '19 at 22:21
  • Is there an issue with making another path that just looks for `client/` and points to the `ClientEditComponent`? – CTheCheese Jan 05 '19 at 22:25
  • Nope, that wouldn't be an issue. – Gargoyle Jan 05 '19 at 22:28
  • Great, for future reference: People who downvote but don't comment could be doing it for a myriad of reasons, but the two common ones are that a question is (Lacking Research | Unclear | Not Useful) or they just break good practices. In your case, being able to copy and paste the line of code you posted and deleting `:id` makes this not that useful to the community. I'm not trying to demean or offend, just giving you my perspective of the downvotes. – CTheCheese Jan 05 '19 at 22:34
  • 1
    But the true question here is understanding what the right way to pull that ID is. – Gargoyle Jan 05 '19 at 22:36

1 Answers1

0

paramMap provides you with a way of pulling the parameters (like :id) from the url. It also provides a has(name: string) function which allows for easy testing to check if a parameter even exists and event a get(name: string) function for getting the value of a parameter. Check out the documentation for more details.

CTheCheese
  • 362
  • 2
  • 12