14_watch.html 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. computed:{
  26. fullName: function(){
  27. console.log(333)
  28. return this.firstName + this.lastName
  29. }
  30. },
  31. methods:{
  32. change(){
  33. this.firstName = 'li'
  34. }
  35. },
  36. watch:{
  37. firstName: function(){
  38. console.log(111)
  39. },
  40. lastName: function(){
  41. console.log(222)
  42. },
  43. sex: function(){
  44. console.log(555)
  45. }
  46. /*
  47. 在Vue中 watch和computed都是用来观察数据变化的
  48. 作用不相同
  49. watch是响应数据变化并且进行相应的操作
  50. computed是用来计算一些基于响应式数据的操作
  51. computed是带有一定缓存的 当响应式没有变化的时候
  52. computed不会重新计算
  53. */
  54. }
  55. })
  56. </script>
  57. </body>
  58. </html>