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

纵然是瞬间 提交于 2019-12-05 14:41:13

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.

The easiest way to do it is:

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

Cant you simply call the methods inside the functions?

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