<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>组件通信:使用v-model</title>
</head>
<body>
<!--自动识别最新稳定版本的Vue.js-->
<script src="https://unpkg.com/vue/dist/vue.min.js"></script>
<div id="app">
<p>总数:{{ total }}</p>
<my-component v-model="total"></my-component>
</div>
<script>
Vue.component('my-component',{
template:'<button @click="handleClick">+1</button>',
data:function(){
return {
counter:0
}
},
methods:{
handleClick:function(){
this.counter++;
this.$emit('input',this.counter)
}
}
});
var app = new Vue({
el:'#app',
data:{
total:0
}
});
</script>
</body>
</html>

组建通信v-model.png
网友评论