0

I have a vue application that has a simple form section, for example, these two inputs:

            <div class=" form-group col-lg-6">
                <label>Name</label>
                <input v-model="newUserName" class="form-control" type="text" name="newUserName">
            </div>
            <div class=" form-group col-lg-6">
                <label>Email</label>
                <input v-model="newUserEmail" class="form-control" type="text" name="newUserEmail">
            </div>

So I have them set to their own v-models, and when I dump those in submission they indeed show the correct input values separately. the problem is, I want to use array.push or something similar so that, when the submit function is hit, it pushes them into a single 'details' array.

So for the example below, i want to push name and email to the details array and only show the array with both values in the console

    data() {
    return {
        details: [],
        newUserName:'',
        newUserEmail: '',
    }
},
methods: {
    showDetails() {

        let data = {
            details: this.details
        };
        console.log(data);
    }
}

1 Answer 1

1

Well, you can just push them to the array. When clicking on the submit button you can call some method submit() for example:

submit() {
this.details.push(this.newUserName)
this.details.push(this.newUserEmail)

console.log(this.details)
}

If you want to reset it on every submit you can just add this.details = [] in the begining of the method

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

3 Comments

Thank you! That worked, and setting the array in the beginning of the method was definitely needed for that reset, thanks!
Can I explicitly push them to an index like 'value'? so that I would get 0: values: "email"
Well you maybe just need to wrap them in one object by default. Your details can be an object with the properties inside and you can just bind the inputs with v-model to it e.g. v-model="details.newUserName" and use the details however you like in the submit.

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.