0

I wanted to make add and edit operation for an entity suppose Post. There are same fields on both add/edit form. So I wanted to ask can I make the form as a separate component and use it in add.vue and edit.vue? Is it the best way to do that kind of thing in VueJS?

I am asking this because I had visited a lot of codes on the internet where the authors didn't follow that approach and they are making separate forms for add/edit.

3 Answers 3

2

Sure you can use same component for both operations. You just need to handle data flow (call correct api / graphql mutation / form action)

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

Comments

0

if you have a form that most of its components are not the same use it in Form.vue and add.vue edit.vue create a base component if it`s reusable

make sure to control the data correctly/state.

Comments

0

You should use one component, as you dont want to duplicate code. It's crucial to pass the id of the entity. If the backend receives the entity without the ID then it know it's the add operation (perform an INSERT in a DB). If the ID is set then the backend knows it has to update.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.