源代碼
點擊運行
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Vue 測試實例 - 教程</title> <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script> </head> <body> <div id = "app"> <p style = "font-size:25px;">計數器: {{ counter }}</p> <button @click = "counter++" style = "font-size:25px;">點我</button> </div> <script type = "text/javascript"> var vm = new Vue({ el: '#app', data: { counter: 1 } }); vm.$watch('counter', function(nval, oval) { alert('計數器值的變化 :' + oval + ' 變為 ' + nval + '!'); }); </script> </body> </html>
運行結果