vue.js data必须是函数

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>vue.js Hello World!</title>
<script type="text/javascript" src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<div id="example-2">
<simple-counter></simple-counter>
<simple-counter></simple-counter>
<simple-counter></simple-counter>
</div>
<script type="text/javascript">
var data = { counter: 0 }
Vue.component('simple-counter', {
template: '<button v-on:click="counter += 1">{{ counter }}</button>',
// 技术上 data 的确是一个函数了,因此 Vue 不会警告,
// 但是我们返回给每个组件的实例的却引用了同一个data对象
data: function () {
return data
}
})
new Vue({
el: '#example-2'
})
</script>
</body>
</html>

posted on 2017-07-13 20:11  醉半仙  阅读(171)  评论(0编辑  收藏  举报

导航