Pass multiple parameter by POST axios vue

20,224

here i got the solution.

<template>
    <form @submit.prevent='onSubmit'>
        <div class="media-comment">
            <input type="text" v-model='comment' class="form-control" placeholder="comment...">
        </div>
    </form>
</template>

<script>
    export default {
        props: ['postId'],

        data() {
            return {
                comment: ''
            }
        },

        methods: {
            onSubmit() {
                axios.post('comments', {comment: this.comment, id: this.postId})
                    .then(post => this.$emit('completed', comment));
            }
        }
    }
</script>
Share:
20,224

Related videos on Youtube

Wahidul Alam
Author by

Wahidul Alam

Updated on July 09, 2022

Comments

  • Wahidul Alam
    Wahidul Alam almost 2 years

    I am trying to pass a props value and an form value to the backend controller using axios. But it only sends form value not the props value. My code is -

    <template>
        <form @submit.prevent='onSubmit'>
            <div class="media-comment">
                <input type="text" v-model='form.comment' class="form-control" placeholder="comment...">
            </div>
        </form>
    </template>
    
    <script>
        export default {
            props: ['postId'],
    
            data() {
                return {
                    form: new Form({comment: ''}),
                    id: this.postId
                }
            },
    
            methods: {
                onSubmit() {
                    console.log(this.postId); // it shows the value in console but the value doesnt pass
                    this.form
                        .post('comments', this.data)
                        .then(post => this.$emit('completed', comment));
                }
            }
        }
    </script>
    

    in console it shows only the comment, not the prop value:

    enter image description here

    How to pass both value ??

    thanks in advance

    • Phorce
      Phorce almost 7 years
      Have you tried '.post({post: this.data, id: this.id}'
  • Bert
    Bert almost 7 years
    comment is not defined because its... not defined. You should use this.$emit('completed', this.comment).
  • Wahidul Alam
    Wahidul Alam almost 7 years
    Thanks brother.. working good but with a huge error .. screenshot : ibb.co/gqWFfQ
  • Duy Anh
    Duy Anh almost 7 years
    @WahidSherief based on the screenshot, it seems that there is a variable "name" in the "dashboard" component that is reactive, and on component load the name is not defined yet (pending request to backend?). To fix you can place the part inside the v-if