Answer the question
In order to leave comments, you need to log in
How can I auto-create variables for v-model?
Recently I came across support for a project that has just a huge number of forms, and caught myself thinking that the code is pretty much the same and only the variables that we track through v-model and url for sending differ.
I thought to write a common mixin for all this, which I am trying to do now. There were no problems with writing common methods, but I got stuck at one point, each form has different input names, and each time I don’t want to fill 10-15 lines in data.
Maybe there is some way to find out which v-models are registered in the component and add them to data automatically.
for example
<tamplate>
<input type="text" v-model="form.user">
<input type="password" v-model="form.password">
</tamplate>
{
form: {
user: ''
password: ''
}
}
Answer the question
In order to leave comments, you need to log in
It is possible not to store field values in data if they are needed only for sending.
<form @submit.prevent='onSubmit'>
<input type="text" name="user">
<input type="password" name="password">
<button type='submit'>Отправить</button>
</form>
async onSubmit($event) {
const fd = new FormData($event.target);
const response = await this.$axios.post('api/form', fd);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question