[TOC]
效果图:
![](https://box.kancloud.cn/fe2587935f397756eee8a22b69b39d07_254x161.gif)
~~~
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17-beta.0/vue.js"></script>
<style>
.one{
background: #c2c2c2;
}
.two{
background: orange;
}
</style>
</head>
<body>
<div id="app">
<like></like>
</div>
<template id="like-component">
<button @click="toggle_like" v-bind:class="liked?'one':'two'">👍🏻 {{like_count}}</button>
</template>
<script>
Vue.component('like',{
template:"#like-component",
data:function(){
return{
like_count:10,
liked:"true"
}
},
methods:{
toggle_like:function(){
this.liked=!this.liked;
if(!this.liked){
this.like_count++
}else{
this.like_count--
}
}
}
});
new Vue({
el: '#app'
})
</script>
</body>
</html>
~~~