【发布时间】:2018-06-01 14:16:15
【问题描述】:
所以我正在使用 vuejs 并尝试添加三元运算符或不会强制应用 v-bind 类的东西。即使数据是假的,它仍然应用 css 类。如何防止这种情况发生?
new Vue({
el: '#cal',
data: {
January: 'no',
February: 'no',
March: 'no',
April: 'yes',
May: 'yes',
June: 'yes',
July: 'no',
August: 'no',
September: 'no',
October: 'no',
November: 'no',
December: 'no'
}
})
table,
th,
td {
border: 1px solid black;
}
th {
width: 100px;
height: 100px;
background-color: white;
}
td {
background-color: grey;
}
.January {
background-color: pink;
}
<script type="text/javascript" src="https://unpkg.com/vue@2.0.3/dist/vue.js"></script>
<table>
<div id="cal">
<tr>
<th>Jan</th>
<th>Feb</th>
<th>Mar</th>
<th>Apr</th>
<th>May</th>
<th>Jun</th>
<th>Jul</th>
<th>Aug</th>
<th>Sep</th>
<th>Oct</th>
<th>Nov</th>
<th>Dec</th>
</tr>
<tr>
<td v-bind: class="{'January' : January == 'yes' }">askjdldsaa</td>
<td v-bind: class="{'February' : February == 'yes'}"></td>
<td v-bind: class="{'March' : March == 'yes'}"></td>
<td v-bind: class="{'April' : April == 'yes'}"></td>
<td v-bind: class="{'May' : May == 'yes'}"></td>
<td v-bind: class="{'June' : June == 'yes'}"></td>
<td v-bind: class="{'July' : July == 'yes'}"></td>
<td v-bind: class="{'August' : August == 'yes'}"></td>
<td v-bind: class="{'September' : September == 'yes'}"></td>
<td v-bind: class="{'October' : October == 'yes'}"></td>
<td v-bind: class="{'November' : November == 'yes'}"></td>
<td v-bind: class="{'December' : December == 'yes'}"></td>
</tr>
</div>
</table>
我已更新示例以显示应用条件的问题。即使我将数据更改为“否”,它仍然会呈现粉色框。
new Vue({
el: '#cal',
data: {
January: 'no',
February: 'no',
March: 'no',
April: 'yes',
May: 'yes',
June: 'yes',
July: 'no',
August: 'no',
September: 'no',
October: 'no',
November: 'no',
December: 'no'
}
})
table,
th,
td {
border: 1px solid black;
}
th {
width: 100px;
height: 100px;
background-color: white;
}
td {
background-color: grey;
}
.January {
background-color: pink;
}
<script type="text/javascript" src="https://unpkg.com/vue@2.0.3/dist/vue.js"></script>
<table>
<div id="cal">
<tr>
<th>Jan</th>
<th>Feb</th>
<th>Mar</th>
<th>Apr</th>
<th>May</th>
<th>Jun</th>
<th>Jul</th>
<th>Aug</th>
<th>Sep</th>
<th>Oct</th>
<th>Nov</th>
<th>Dec</th>
</tr>
<tr>
<td v-bind: class="{'January' : January == 'yes' }">askjdldsaa</td>
<td v-bind: class="{'February' : February == 'yes'}"></td>
<td v-bind: class="{'March' : March == 'yes'}"></td>
<td v-bind: class="{'April' : April == 'yes'}"></td>
<td v-bind: class="{'May' : May == 'yes'}"></td>
<td v-bind: class="{'June' : June == 'yes'}"></td>
<td v-bind: class="{'July' : July == 'yes'}"></td>
<td v-bind: class="{'August' : August == 'yes'}"></td>
<td v-bind: class="{'September' : September == 'yes'}"></td>
<td v-bind: class="{'October' : October == 'yes'}"></td>
<td v-bind: class="{'November' : November == 'yes'}"></td>
<td v-bind: class="{'December' : December == 'yes'}"></td>
</tr>
</div>
</table>
【问题讨论】:
-
您的组件中有 this.January 吗?你能展示你的javascript代码吗?
标签: vue.js