vue.js - 单击时如何更改按钮的颜色?(Vue)
问题描述
我试图获得一个按钮,当按下它时会改变它的颜色。当再次按下它时,它应该变回原来的颜色。我究竟做错了什么?
我模板中的按钮:
<th><Button v-bind:class="{'white': !clicked, 'blue': clicked}" v-on:click ="!clicked" ></Button></th>
<script>
export default {
data: {
clicked: false
}
}
</script>
<style>
.white {
background-color: white;
width: 200px;
height: 200px;
}
.blue {
width: 200px;
height: 200px;
background-color: blue;
}
</style>
解决方案
您应该clicked
通过以下方式明确设置属性@click="clicked = !clicked"
:
<th>
<Button
v-bind:class="{'white': !clicked, 'blue': clicked}"
v-on:click ="clicked = !clicked"
/>
</th>
推荐阅读
- python - 查找列表中的最大数量
- server - 获取路由器配置页面而不是我的网站主页
- url - 更新记录后如何返回上一页?
- npm - 使用“npm install”时出现错误“无法读取未定义的属性“匹配””
- yaml - 在 azure Devops 中创建 Yaml 管道时出现问题
- ios - 如何在objc中使非指针类型属性为空
- c# - 在 .net core 2.1 webapi 应用程序上,是否可以配置 NLog 而无需使用 Main 方法或 Startup.cs 中的任何方法进行配置?
- python - Python Timedelta[M] 添加不完整的天数
- amazon-dynamodb - 读取 DynamoDB 中的最新记录
- android - 未来建造者
(脏,状态:_FutureBuilderState #7140d) 在 null 上调用了 getter 'isEmpty'