4.天气案例_监视属性_简写.html 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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>天气案例_监视属性_简写</title>
  8. <!-- 引入vue -->
  9. <script type="text/javascript" src="../js/vue.js"></script>
  10. </head>
  11. <body>
  12. <!-- 准备好一个容器 -->
  13. <div id="root">
  14. <h2>今天天气很{{info}}</h2>
  15. <button @click="changeWeathers">切换天气</button>
  16. </div>
  17. </body>
  18. <script type="text/javascript">
  19. Vue.config.productionTip = false //阻止 vue 在启动时生成生产提示
  20. const vm = new Vue({
  21. el:'#root',
  22. data:{
  23. isHot:true
  24. },
  25. computed:{
  26. info(){
  27. return this.isHot ? '炎热' : '凉爽'
  28. }
  29. },
  30. methods: {
  31. changeWeathers(){
  32. this.isHot = !this.isHot
  33. }
  34. },
  35. watch:{
  36. //正常写法
  37. // isHot:{
  38. // // immediate:true, //初始化时让handler调用一下
  39. // // deep:true, //深度监视
  40. // handler(newValue,oldValue){
  41. // console.log('isHot被修改了',newValue,oldValue)
  42. // }
  43. // },
  44. // 简写
  45. // isHot(newValue,oldValue){
  46. // console.log('isHot被修改了',newValue,oldValue)
  47. // }
  48. }
  49. })
  50. // 正常写法
  51. // vm.$watch('isHot',{
  52. // immediate:true, //初始化时让handler调用一下
  53. // // deep:true, //深度监视
  54. // handler(newValue,oldValue){
  55. // console.log('isHot被修改了',newValue,oldValue)
  56. // }
  57. // })
  58. // 简写
  59. vm.$watch('isHot',function(newValue,oldValue){
  60. console.log('isHot被修改了',newValue,oldValue)
  61. })
  62. </script>
  63. </html>