美文网首页
Vue学习笔记五:侦听属性watch

Vue学习笔记五:侦听属性watch

作者: 开发者连小超 | 来源:发表于2019-06-19 16:14 被阅读0次
    <template>
      <div class="box">
        <p>今日温度:{{wendu}}℃</p>
        <p>穿衣建议:{{clothing}}</p>
        <p><button @click="up">升高温度</button><button @click="down">降低温度</button></p>
      </div>
    </template>
    <script>
    export default {
      data () {
        return {
         wendu:14,
          clothing:'夹克长裙',
          clothings: ['T恤短袖','夹克长裙','棉衣羽绒服']
        }
      },
      methods:{
        up() {
            this.wendu += 5;
        },
        down() {
            this.wendu -= 5;
        }
      },
      watch:{//数据监控
        wendu(newVal,oldVal) {
            if (newVal >= 26) {
                this.clothing = this.clothings[0];
            } else if(newVal < 26 && newVal >= 0){
                this.clothing = this.clothings[1];
            } else {
                this.clothing = this.clothings[2];
            }
        }
      }
    }
    </script>
    

    相关文章

      网友评论

          本文标题:Vue学习笔记五:侦听属性watch

          本文链接:https://www.haomeiwen.com/subject/zjmkqctx.html