12_computed.html 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <script src="./js/vue.js"></script>
  8. </head>
  9. <body>
  10. <div id="app">
  11. <h1>
  12. {{num1}} + {{num2}} = {{sum}}
  13. </h1>
  14. <button @click="changeFun">change</button>
  15. <h1>{{sum2}}</h1>
  16. </div>
  17. <script>
  18. let app = new Vue({
  19. el: '#app',
  20. data:{
  21. num1:1,
  22. num2:2
  23. },
  24. methods: {
  25. changeFun:function(){
  26. // this.num2 = 100;
  27. this.sum2 = 1000;
  28. }
  29. },
  30. computed:{
  31. sum:function(){
  32. console.log("computed运行")
  33. let thisSum = this.num1 + this.num2;
  34. return thisSum;
  35. },
  36. sum2:{
  37. get(){
  38. console.log("get");
  39. return this.num1 + this.num2;
  40. },
  41. set(val){
  42. console.log(val);
  43. console.log("set")
  44. this.num1 = 998
  45. }
  46. }
  47. }
  48. })
  49. </script>
  50. </body>
  51. </html>