简单教程
提交运行
代码编辑器:
<!DOCTYPE html> <meta charset="utf-8"> <title>Vue 2 范例 - 简单教程(www.twle.cn)</title> <link href="/static/next/css/tryit.css?v=2017082407" rel="stylesheet"/> <script src="/static/lib/vue.js/2.5.9/vue.min.js"></script> <div id="app"> <div id="counter-event-example"> <p>{{ total }}</p> <button-counter v-on:increment="incrementTotal"></button-counter> <button-counter v-on:increment="incrementTotal"></button-counter> </div> </div> <footer>简单教程,简单编程<br/>Copyright © 简单教程</footer> <script> Vue.component('button-counter', { template: '<button v-on:click="incrementHandler">{{ counter }}</button>', data: function () { return { counter: 0 } }, methods: { incrementHandler: function () { this.counter += 1 this.$emit('increment') } }, }) new Vue({ el: '#counter-event-example', data: { total: 0 }, methods: { incrementTotal: function () { this.total += 1 } } }) </script>
运行结果: