2017-04-10 11 views
0

この変数とメソッドとの間の違いは何ですか?方法と作成方法の違いは何ですか?

グローバル変数personas、apis、totalをメソッドhandleCurrentChangeで変更したい場合は、コンテキストを保存する必要があります。

しかし、なぜこれを直接作成して使用できますか?

var vue = new Vue({ 
     el: "#app", 
     personas: {}, 
     apis: {}, 
     total: '', 
     methods: { 
      submitForm: function(formName) { 
       var _this = this; 
       this.$refs[formName].validate(function(valid) { 
        if (valid) { 
         console.log('form validate successfule.'); 
        } else { 
         console.log('form validate failed.'); 
        } 
       }); 
      }, 
      handleCurrentChange: function (val) { 
       var _this = this; 
       this.$http.post('/persona/index', { 
        current: val, 
        size: this.pageSize 
       }, {emulateJSON: true}).then(function (response) { 
        _this.personas = response.body.data.personas; 
        _this.total = response.body.data.total; 

       }, function (response) { 
        console.log('Sorry, there's no response.'); 
       }); 
      } 
     }, 
     created: function() { 
      this.$http.get('/persona/index').then(function (res) { 
       this.personas= res.body.data.personas; 
       this.total = res.body.data.total; 
       this.apis= res.body.data.apis; 
      }); 
     } 
    }); 

答えて

0

あなたは、これらの機能にthisを使用したい場合、あなたは、単に(vuejsに固有のものではないES6、)矢印の機能を使用することができます。

this.$refs[formName].validate(valid => { 
    // you can use 'this' here 
}); 

arrow functionsについては、こちらをご覧ください。

関連する問題