请陛下批阅
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>为什么组件中的data是一个function</title>
<script src="../../vue/vue-v2.6.11.js"></script>
</head>
<body>
<div id="app">
<h1 style="color: red">
<p>为什么data要定义为一个function?</p>
<p>答: 解决数据共享问题,使得每个组件操作的数据互不干扰!!!</p>
</h1>
<mycom></mycom>
<hr/>
<mycom></mycom>
<hr/>
<mycom></mycom>
<hr/>
</div>
<template id="tmpl">
<div>
<button @click="increment">+1</button>
<h3>{{count}}</h3>
</div>
</template>
</body>
<script>
Vue.component('mycom', {
template: '#tmpl',
data: function () {
return {count: 0};
},
methods: {
increment() {
this.count++;
}
}
});
var vm = new Vue({
el: '#app',
data: {}
});
</script>
</html>