How to bind events in vue.js: 1. Write the js method directly in the tag, the code is []; 2. The method of calling method.
【Recommended related articles: vue.js】
vue.js binding Methods for defining events:
1. Write js methods directly in tags
<button v-on:click="alert('hi')">执行方法的第一种写法</button>
2. Methods for calling methods
<button v-on:click="run()">执行方法的第一种写法</button> <button @click="run()">执行方法的 简写 写法</button> export default { data () { return { msg: '你好vue', list:[] } }, methods:{ run:function(){ alert('这是一个方法'); } } }
(1) Method passing parameters, the method directly passes the parameters in the method when calling
<button @click="deleteData('111')">执行方法传值111</button> <button @click="deleteData('222')">执行方法传值2222</button>
(2) Passing in the event object
<button data-aid='123' @click="eventFn($event)">事件对象</button>
eventFn(e){ console.log(e); // e.srcElement dom节点 e.srcElement.style.background='red'; 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!