使用Vue中的组件创建一个计数器
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div id="app">
<ul>
<li><my-counter></li>
<li><my-counter></li>
<li><my-counter></li>
</ul>
</div>
<script type="text/javascript">
Vue.component('my-counter',{
template:`<div>
<button @click='minus'>-</button><span>{{n}}</span><button @click='add'>+</button>
</div>`,
data(){
return {
n:1
}
},
methods:{
minus(){
this.n--;
this.n==0&&(this.n=1)
},
add(){
this.n++
}
}
})
</script>
<script type="text/javascript">
new Vue({
el:"#app"
})
</script>
</body>
</html>