<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<my-component></my-component>
<my-component></my-component>
</div>
<script>
var data1={
counter:0
}
Vue.component("my-component",{
template:"<button @click='count'>点击事件{{counter}}</button>",
data:function(){
//return 处的对象引用了外部对象,那么这个对象就是共享对象
return data1
},
methods:{
count:function(){
data1.counter++;
}
}
}
);
var v=new Vue({
el:"#app"
});
</script>
</body>
</html>