8_计算属性和监听器.html 944 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. watch:{
  30. firstName: function(){
  31. console.log(111)
  32. },
  33. lastName: function(){
  34. console.log(222)
  35. }
  36. },
  37. computed:{
  38. fullName: function(){
  39. return this.firstName + this.lastName
  40. }
  41. }
  42. })
  43. </script>
  44. </body>
  45. </html>