Issue
I wonder if there is any reason to use
this.someProperty = this.route.snapshot.paramMap.get('someParam');
instead of
this.route.paramMap.subscribe((param) =>
{
this.someProperty = param.get('someParam');
});
The same goes for queryParamMap.
In a tutorial I am following (by Deborah Kurata on Pluralsight) they seem to use snapshot by default and subscribe only when you expect the param to change while the route stays the same. But applications tend to change over time. Changes can introduce a situation where the param changes while the route stays the same. It is easy to overlook this situation.
So should it be (or is it) best practice to use subscribe instead of snapshot?
Snapshot way is shorter to write, but are there any other benefits?
I may be asking for opinions here, but I want to know if there is an official best practice.
Thanks in advance.
Solution
when using snapshot
you get the value once and that is it, if the user changes the parameters in the url, or using navigate to same route with another parameter, you don't get the new value (unless you are reading it each time you need it)
Also snapshot
is sync
(versus subscribe with is async)
if you subscribe
each time the value is changed you are able to read the new values.
Answered By - Reza
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.