源代碼
點擊運行
<!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;">計數器: {{ products.id }}</p> <button @click = "products.id++" style = "font-size:25px;">點我</button> </div> <script type = "text/javascript"> var myproduct = {"id":1, name:"book", "price":"20.00"}; var vm = new Vue({ el: '#app', data: { counter: 1, products: myproduct } }); Vue.delete(myproduct, 'price'); console.log(vm); vm.$watch('counter', function(nval, oval) { alert('計數器值的變化 :' + oval + ' 變為 ' + nval + '!'); }); </script> </body> </html>
運行結果