[英]Event Listener in Vue.js
I'm currently building a small alarm application with Vue.js.我目前正在使用 Vue.js 构建一个小型警报应用程序。 I want to have an eventListener on buttons with a class of "del" that calls a method and hands over the event, I'm using Vue's "mounted" feature for that:我想在具有“del”类的按钮上有一个 eventListener 来调用方法并移交事件,我为此使用了 Vue 的“挂载”功能:
mounted: function timeInterval () {
var app = this;
var del = document.getElementsByClassName("del");
del.addEventListener('click', function (e) {
app.deleteAlarm(e);
},
}
In that method I want to get the id of the button that was clicked and do something with it:在该方法中,我想获取单击的按钮的 id 并对其进行处理:
deleteAlarm: function (e) {
var app = this;
var id = e.target.id;
app.alarms.splice(id, 1);
}
I spent hours on figuring out what's going wrong but I can't get it.我花了几个小时弄清楚出了什么问题,但我无法理解。
Edit: The way I want to do this is important, because the buttons are part of a dynamic list, that gets rendered via v-html.编辑:我想这样做的方式很重要,因为按钮是动态列表的一部分,通过 v-html 呈现。 This is the method that adds the HTML to the data variable:这是将 HTML 添加到数据变量的方法:
getAlarmList: function () {
var app = this;
app.alarmTable = '';
for (let i=0; i<app.alarms.length; i++) {
app.alarmTable += "<tr><td>"+app.alarms[i][0]+"</td><td>"+app.alarms[i][1]+":"+app.alarms[i][2]+":"+app.alarms[i][3]+"</td><td><button type=\"button\" id=\""+i+"\" class=\"btn btn-dark btn-sm del\">Löschen</button></td></tr>";
}
And this is how the variable gets rendered out with the v-html directive:这就是使用 v-html 指令呈现变量的方式:
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Time</th>
<th></th>
</tr>
</thead>
<tbody v-html="alarmTable">
</tbody>
</table>
I'm not sure how to go about the event listener approach, but I think using the v-for directive with a vue template would allow you to do what you need.我不确定如何使用事件侦听器方法,但我认为将 v-for 指令与 vue 模板一起使用将允许您执行所需的操作。
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Time</th>
<th></th>
</tr>
</thead>
<tbody v-html="alarmTable">
<template v-for="(alarm, index) in alarms">
<tr>
<td>{{ alarm[0] }}</td>
<td>{{ alarm[1] }}:{{ alarm[2] }}:{{ alarm[3] }}</td>
<td>
<button
type="button"
v-on:click="deleteAlarm(index)"
class="btn btn-dark btn-sm"
>
Löschen
</button>
</td>
</tr>
<template>
</tbody>
</table>
Then you could workshop your deleteAlarm()
function to either delete the row, or delete the item from the alarms
array.然后,您可以修改deleteAlarm()
函数以删除该行或从alarms
数组中删除该项目。
This example demonstrates an event listener and a binding.此示例演示了一个事件侦听器和一个绑定。
<div id="app">
<button v-on:click="del" id="1" class="deletable">Foo</button>
<button v-on:click="del" id="2" class="deletable">Bar</button>
</div>
new Vue({
el: '#app',
mounted () {
this.$el.querySelectorAll('.deletable').forEach( (button) => {
button.addEventListener('click', (e) => {
console.log("delete from event handler:" + e.target.id);
})
} );
},
methods: {
del (e) {
console.log("delete from a method:" + e.target.id);
}
}
})
Update : I updated the fiddle to demo both, the binding and an even listener.更新:我更新了小提琴以演示绑定和偶数侦听器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.