Kenziiee Flavius
Kenziiee Flavius

Reputation: 1949

Pass Vue Variable to Vue Function

I need to pass a VueJS variable to a VueJs function without the page submitting. For some reason the form is still submitting after passing the variable through to the function.

HTML

<div class="list-group-item" v-for="event in events">
    <form method="POST" v-on:submit.prevent="deleteEvent('@{{ event.id }}')")>
        <b><h1>@{{ event.name }}</h1></b>
        <hr>
        <small>@{{ event.description }}</small>
        <hr>
        <b>@{{ event.date }}</b>
        <hr>
        <button class="btn btn-danger form-control">Delete</button>
    </form>
</div>

JavaScript

new Vue({

el: '#app',

data: {
    newEvent: {
        name: '',
        description: '',
        date: ''
    }
},

ready: function(){
    this.retrieveEvents();
},

methods: {

    retrieveEvents: function(){
        this.$http.get('/retrieveEvents', function(response){
            this.$set('events', response);
        });
    },

    addEvent: function(e){
        e.preventDefault();
        var event = this.newEvent;
        this.$http.post('/addEvent', event, function(){
           this.newEvent = {name: '', description: '', date: ''};
        });
        this.retrieveEvents();
    },

    deleteEvent: function(id){
        e.preventDefault();
        console.log(id);
    }

}
});

I don't see why it keeps submitting the form and not passing the variable into VuejJS, everything else works perfectly.

Upvotes: 0

Views: 3897

Answers (1)

elclanrs
elclanrs

Reputation: 94101

You don't need to interpolate in the binding syntax, you can access the properties directly:

<form method="POST" v-on:submit.prevent="deleteEvent(event.id)">

Upvotes: 1

Related Questions