javascript - Vue 方法总是被调用(不仅仅是点击)
问题描述
我对 vue 很陌生,但仍在尝试了解我的代码背后的基础知识。现在我的方法有问题。仅当用户单击按钮时才应调用它。但相反,它总是被调用。我添加了一个 alert()/console.log(),然后多次显示。
这里有一些代码:
<template>
<div>
<center>
<table>
<tr>
<th><button :on-click="click(1)" class="white1"><li v-bind:class="{'icon': containertyp[1] == 'l', 'iconLH': containertyp[1] == 'lh', 'iconBH': containertyp[1] == 'bh'}"></li></button></th>
<th><button :on-click="click(2)" class="white1"><li v-bind:class="{'icon': containertyp[2] == 'l', 'iconLH': containertyp[2] == 'lh', 'iconBS': containertyp[2] == 'bs'}"></li></button></th>
<th><button :on-click="click(3)" class="white1"><li v-bind:class="{'icon': containertyp[3] == 'l', 'iconLH': containertyp[3] == 'lh', 'iconBS': containertyp[3] == 'bs'}"></li></button></th>
<tr>
</table>
</center>
</div>
</template>
export default {
data: function () {
return {
nr: [],
containertyp: [],
}
},
methods: {
click(number) {
for (var i = 0; i < 27; i++) {
this.nr[i] = false;
if (number == i) {
this.nr[i] = true;
}
};
console.log(this.nr);
EventBus.$emit('containerclicked');
}
}
}
解决方案
这个属性是不同语法的奇怪混合:
:on-click="click(1)"
目前尚不清楚您是在尝试绑定onclick
元素的属性还是(更有可能)向元素添加 Vue 点击侦听器。
您真正想要的很可能是这样的:
@click="click(1)"
The@
是 的简写v-on:
,而:
原始代码中的the 是 的简写v-bind:
。尝试绑定一个名为的属性on-click
是完全有效的,但它将被视为自定义属性,因为on-click
它实际上并不是一个东西。在渲染期间评估绑定以确定属性的值,这就是您将在渲染期间看到日志记录的原因。
推荐阅读
- pdf - PDF 语法 BT ET 文本操作
- php - 如何更新表中的多行
- google-signin - 谷歌在 Android Things 中登录非常非常慢
- vba - 在隐藏应用程序的任务栏上显示 Word 用户窗体
- spring-webflux - Spring webflux,ServerRequest获取客户端/浏览器IP地址
- android - 在这里 api 的任何地方都找不到许可证密钥
- google-bigquery - 在 bigquery 上进行查询,这会消耗大量时间
- php - 更新 MySQL 后,为空的“浮点”字段添加了 0(零)而不是 null
- sql - sql order by 不正确的语法
- c# - 为什么我不能使用 `WTSVirtualChannelQuery` 返回的句柄?