index.html 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
  6. <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  7. </head>
  8. <body>
  9. <div id = "app">
  10. 千米 : <input type = "text" v-model = "kilometers">
  11. 米 : <input type = "text" v-model = "meters">
  12. </div>
  13. <p id="info"></p>
  14. <script type = "text/javascript">
  15. var vm = new Vue({
  16. el: '#app',
  17. data: {
  18. kilometers : 0,
  19. meters:0
  20. },
  21. methods: {
  22. },
  23. computed :{
  24. },
  25. watch : {
  26. kilometers:function(val) {
  27. this.kilometers = val;
  28. this.meters = val * 1000;
  29. },
  30. meters : function (val) {
  31. this.kilometers = val/ 1000;
  32. this.meters = val;
  33. }
  34. }
  35. });
  36. // $watch 是一个实例方法
  37. vm.$watch('kilometers', function (newValue, oldValue) {
  38. // 这个回调将在 vm.kilometers 改变后调用
  39. document.getElementById ("info").innerHTML = "修改前值为: " + oldValue + ",修改后值为: " + newValue;
  40. })
  41. </script>
  42. </body>
  43. </html>