<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>todos</title>
<script src="https://cdn.bootcss.com/vue/2.4.2/vue.js"></script>
<style>
.box {
width: 100px;
height: 100px;
border: 10px solid #ccc;
}
.active {
background: red
}
</style>
</head>
<body>
<div class="wrap" id="todos">
<div class="box" :class='{active:isActive}' @click='setBG'>
</div>
</div>
</body>
<script>
var todos = new Vue({
el: '#todos',
data: {
isActive: false
},
methods: {
setBG() {
this.isActive = !this.isActive
console.log(this.isActive)
}
}
});
</script>
</html>
this.isActive = !this.isActive //主要是运用到这个取反,用途点击收藏时不同的效果