8_watch.html 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. var app = new Vue({
  19. el: "#app",
  20. data: {
  21. firstName: 'zhang',
  22. lastName: 'san',
  23. sex: '男'
  24. },
  25. methods: {
  26. change() {
  27. this.firstName = 'li'
  28. }
  29. },
  30. computed: {
  31. fullName: function () {
  32. console.log(333)
  33. return this.firstName + this.lastName
  34. }
  35. },
  36. watch: {
  37. firstName: function(){
  38. console.log('fffff')
  39. },
  40. lastName: function(){
  41. console.log('llllll')
  42. }
  43. }
  44. /*
  45. 在vue中 watch和computed都是用来观察数据变化的
  46. 作用不同
  47. watch是响应式数据变化并且进行相应的操作
  48. computed是用来计算一些基于响应式数据的操作
  49. computed 是带有一定缓存的 当响应式数据没有变化的时候 computed 不会重新计算
  50. */
  51. })
  52. </script>
  53. </body>
  54. </html>