As I develop a spa app using vue js, I have set up these routes:
{ path: '/artikel/create',name: 'artikelCreate', components: { default: artikel_form, 'header': header} },
{ path: '/artikel/edit/:id',name: 'artikelEdit', components: { default: artikel_form, 'header': header}, meta: { mode:'edit' } },
The artikelCreate
route creates a blank form from the artikel_form
component, while the artikelEdit
route populates the form with data based on the :id
. Both use the same artikel_form
component.
This setup allows me to avoid creating two very similar form components. By checking this.$route.meta.mode === 'edit'
, I can determine whether the route is for editing or creating an article and perform relevant actions.
However, I encountered an issue when following these steps:
- Navigating to the
artikelEdit
route displays the populatedartikel_form
component. - Despite having a navigation bar, moving from the
artikelEdit
route to theartikelCreate
route does not result in a new empty form being created as expected. - Even after changing routes, the
artikel_form
component remains from theartikelEdit
route instead of resetting for theartikelCreate
route.
Is there a more elegant way to reset or recreate components in vue, rather than just cleaning out input bindings like v-model
? This method may become cumbersome for components with complex dependencies beyond basic text inputs in a form, and it also leaves room for errors if each input must be specified individually.
Any suggestions on how to reload, refresh, or entirely recreate a component in vue?