2-way binding of URL params to v-model
#2318
seascallion
started this conversation in
Ideas
Replies: 1 comment
-
Currently, the existing solution is to use a composable like |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
What problem is this solving
Vue router's
props
property allows mapping URL query and route params to component props. This is somewhat useful, as it allows decoupling the component from any route-handling logic, but it only supports props, i.e. one-way bindings. We often want to have values that are 2-way bound (i.e. model), both loaded from the URL (either as route params or query params) and saved back to the URL when they change. It would be nice not to have towatch
these values androuter.replace({ ... })
within the component, as that introduces the same route coupling into the component, that theprops
property aims to eliminate.If this is not feasible for any reason, would appreciate pointers on best practices on how to handle syncing values with the URL in a decoupled way.
Proposed solution
This could be accomplished by having another property,
events
, within the route record to handle events emitted from the component:It would be very useful for events to work with typescript as well.
Describe alternatives you've considered
I have used these methods to accomplish 2-way binding to the URL:
computed
property with a getter and setter defined to load and save from URL paramsref
that is populated from the URL and awatch
that usesrouter.replace(...)
to update the URL when the value changesI believe the proposed solution - handling these bindings within the routing itself - can be cleaner than either of these approaches.
Beta Was this translation helpful? Give feedback.
All reactions