The first way to write the execution method]; 2. How to call method."/> The first way to write the execution method]; 2. How to call method.">

Home  >  Article  >  Web Front-end  >  How to bind events in vue.js

How to bind events in vue.js

coldplay.xixi
coldplay.xixiOriginal
2020-11-13 14:48:003598browse

How to bind events in vue.js: 1. Write the js method directly in the tag, the code is [f385b30ca3dc50ec4a2a59876cb41656 execution method The first way of writing 65281c5ac262bf6d81768915a4a77ac0]; 2. The method of calling method.

How to bind events in vue.js

【Recommended related articles: vue.js

vue.js binding Methods for defining events:

1. Write js methods directly in tags

 <button v-on:click="alert(&#39;hi&#39;)">执行方法的第一种写法</button>

2. Methods for calling methods

<button v-on:click="run()">执行方法的第一种写法</button>
<button @click="run()">执行方法的 简写 写法</button>
export default {     
      data () { 
        return {
          msg: &#39;你好vue&#39;,
          list:[]      
        }
      },
      methods:{
           run:function(){
              alert(&#39;这是一个方法&#39;);
             }
           }
 }

(1) Method passing parameters, the method directly passes the parameters in the method when calling

<button @click="deleteData(&#39;111&#39;)">执行方法传值111</button>
<button @click="deleteData(&#39;222&#39;)">执行方法传值2222</button>

(2) Passing in the event object

<button data-aid=&#39;123&#39; @click="eventFn($event)">事件对象</button>
eventFn(e){
          console.log(e);
          // e.srcElement  dom节点
          e.srcElement.style.background=&#39;red&#39;;
          console.log(e.srcElement.dataset.aid);  /*获取自定义属性的值*/
        }

Related free learning Recommended: JavaScript(Video)

The above is the detailed content of How to bind events in vue.js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn