<!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>
</head>
<body>
<div id="app">
<cpn1></cpn1>
<cpn1></cpn1>
<cpn1></cpn1>
</div>
<template id="cpn1">
<div>
<h2>nihao </h2>
{{count}}
<button @click="increase">+</button>
<button @click="decrease">-</button>
</div>
</template>
<script src="vue.js"></script>
<script>
Vue.component('cpn1', {
template: '#cpn1',
data(){
return {
message:"abc",
count:0
}
},
methods:{
increase(){
this.count++
},
decrease(){
this.count--
}
}
})
const app = new Vue({
el: "#app",
data: {
},
})
</script>
</body>
</html>
当然,虽然组件可以复用,但是组件里的数据是独立的