02_天气案例_监视属性.html 1.4 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. <link rel="icon" href="../../logo.svg" />
  8. <title>天气案例_监视属性</title>
  9. </head>
  10. <body>
  11. <div id="root">
  12. <h2>今天天气很{{info}}</h2>
  13. <button @click="isHot = !isHot">切换天气</button>
  14. </div>
  15. <script src="../../vue.js"></script>
  16. <script>
  17. Vue.config.productionTip = false; // 阻止 vue 在启动时生成生产提示。
  18. const vm = new Vue({
  19. el: "#root",
  20. data: {
  21. isHot: true,
  22. },
  23. methods: {
  24. // changeWeather() {
  25. // this.isHot = !this.isHot;
  26. // },
  27. },
  28. computed: {
  29. info() {
  30. return this.isHot ? "炎热" : "凉爽";
  31. },
  32. },
  33. // watch: {
  34. // isHot: {
  35. // immediate: true, // 初始化时让 handler 调用一下
  36. // handler(currentValue, prevValue) {
  37. // console.log(currentValue, prevValue);
  38. // },
  39. // },
  40. // },
  41. });
  42. vm.$watch("isHot", {
  43. immediate: true, // 初始化时让 handler 调用一下
  44. handler(currentValue, prevValue) {
  45. console.log(currentValue, prevValue);
  46. },
  47. });
  48. </script>
  49. </body>
  50. </html>