【问题标题】:Pass Vue Variable to Vue Function将 Vue 变量传递给 Vue 函数
【发布时间】:2016-02-24 21:36:26
【问题描述】:

我需要在不提交页面的情况下将 VueJS 变量传递给 VueJs 函数。由于某种原因,在将变量传递给函数后,表单仍在提交。

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);
    }

}
});

我不明白为什么它一直提交表单而不是将变量传递给 VuejJS,其他一切都运行良好。

【问题讨论】:

    标签: javascript laravel vue.js


    【解决方案1】:

    你不需要在绑定语法中插值,你可以直接访问属性:

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

    【讨论】:

      猜你喜欢
      • 2021-04-21
      • 1970-01-01
      • 2020-02-11
      • 2019-11-02
      • 2020-04-05
      • 1970-01-01
      • 2018-04-02
      • 2021-02-04
      • 2018-02-17
      相关资源
      最近更新 更多