vue 计数求和counter简单例子
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>test</title><script src="./vue.js&quo
·
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>test</title>
<script src="./vue.js"></script>
<style>
</style>
</head>
<body>
<div id="root">
<counter ref="one" @change='handleChange'></counter>
<counter ref="two" @change='handleChange'></counter>
<div>{{total}}</div>
</div>
<script>
Vue.component('counter', {
template: '<div @click="handleClick">{{number}}</div>',
data: function() {
return {
number: 0
}
},
methods: {
handleClick: function() {
this.number++;
this.$emit('change');
}
}
})
var app = new Vue({
el: " #root ",
data: {
total: 0,
},
methods: {
handleChange: function() {
// console.log(this.$refs.one.number)
this.total = this.$refs.one.number + this.$refs.two.number;
}
}
})
</script>
</body>
</html>
说明 :
1.求和
2.使用ref
3.子组件的data是个函数
更多推荐
已为社区贡献5条内容
所有评论(0)