(四)Vueにおけるwatchの方法

1654 ワード

このセクションの知識点
  • watchメソッド動的傍受変化
  • app.$watch(" xxx",function(newVal,oldVal){})
    

    コードは次のとおりです.
    
    
    
        
        
        Watch option
    
    
    

    Watch option


    :{{temperature}}°C

    :{{this.suggestion}}

    var suggestion=['T ',' ',' ']; var app=new Vue({ el:'#app', data:{ temperature:14, suggestion:' ' }, methods:{ add:function(){ this.temperature+=5; }, reduce:function(){ this.temperature-=5; } }, // watch:{ // temperature:function(newVal,oldVal){ // if(newVal>=26){ // this.suggestion=suggestion[0]; // }else if(newVal<26 && newVal >=0) // { // this.suggestion=suggestion[1]; // }else{ // this.suggestion=suggestion[2]; // } // } // } }) app.$watch('temperature',function(newVal,oldVal){ if(newVal>=26){ this.suggestion=suggestion[0]; }else if(newVal<26 && newVal >=0) { this.suggestion=suggestion[1]; }else{ this.suggestion=suggestion[2]; } })