How to add two functions on a @click event using vue.js?

落花浮王杯 提交于 2019-12-22 08:26:06

问题


This is my code and i basically want to add CHANGEBUTTONS to the on click event that looks like @click.

<button @click="enviarform2" value="Delete from favorites" style="font-weight: 700;color:#428bca;margin-left:30px;height:30px;border-radius:4px" name="delete" v-else>Delete from favorites</button>


<script>
new Vue({
  el:'#app',
  data:{
    show: true,
    paletteid : <?=$palette_id;?>,
    action: "add",
    action2: "delete",
    number: ""
  },
  methods: {
           enviarform: function() {
            axios.post('/validarfavorite.php', {
                paletteid: this.paletteid,
                action: this.action
              })
              .then(function (response) {
                console.log(response);
              })
              .catch(function (error) {
                console.log(error);
              });
              this.number = "Yours plus ";
          },
           enviarform2: function() {
            axios.post('/validarfavorite.php', {
                paletteid: this.paletteid,
                action2: this.action2
              })
              .then(function (response) {
                console.log(response);
              })
              .catch(function (error) {
                console.log(error);
              });
              this.number = "Minus yours plus ";
          },
          changebuttons: function() {
            this.show = !this.show;
          }
        }
});
</script>

I have tried with method 1 and method 2 and handler but it didnt work. Hope you know!


回答1:


In it's simplest form, you can, having the template:

<button @click="handleButtonClick">my button</button>

Do, in your JavaScript Code:

new Vue({
    el:'#app',  
    data:{   
       show: true,
       //    ... 
    },
    methods: {
       m1: function() {   /* ... */ },
       m2: function() { /* ... */ },
       handleButtonClick: function() {
         /* call two methods. */
         this.m1();
         this.m2();
       }
    }
})

You can also use JavaScript's comma operator:

<script src="https://unpkg.com/vue"></script>

<div id="app">
  <p>{{ message }}</p>
  <button @click="m1(), m2()">two methods</button>
</div>
new Vue({
  el: '#app',
  data: {
    message: 'Hello Vue.js!'
  },
  methods: {
    m1() { this.message += "m1"; },
    m2() { this.message += "m2"; }
  }
})

Demo JSFiddle here.




回答2:


The easiest way to do it is:

<button v-on:click="method1(); method2();">Continue</button>



回答3:


Cant you simply call the methods inside the functions?



来源:https://stackoverflow.com/questions/49209834/how-to-add-two-functions-on-a-click-event-using-vue-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!