8_watch.html 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <div id="app">
  11. <button @click="change()">修改</button>
  12. <h2>姓:{{firstName}}</h2>
  13. <h2>名:{{lastName}}</h2>
  14. <h2>姓名:{{fullName}}</h2>
  15. </div>
  16. <script src="vue.js"></script>
  17. <script>
  18. new Vue({
  19. el:'#app',
  20. data:{
  21. firstName:'zhang',
  22. lastName: 'san'
  23. },
  24. methods:{
  25. change(){
  26. this.firstName = 'li'
  27. }
  28. },
  29. computed:{
  30. fullName: function(){
  31. return this.firstName + this.lastName
  32. }
  33. },
  34. watch:{
  35. firstName: function(){
  36. console.log(111)
  37. },
  38. lastName: function(){
  39. console.log(222)
  40. }
  41. }
  42. })
  43. /*
  44. 在Vue中,watch和computed都是用来观察数据变化的
  45. 他们的作用是不同的
  46. watch是响应数据的变化并进行相应的操作
  47. computed是用来计算一些基于响应式数据的数据
  48. computed 是带有缓存的 当响应式数据没有变化的时候
  49. computed不会重新计算
  50. */
  51. </script>
  52. </body>
  53. </html>