方法和创建之间有什么区别



这个变量在方法和创建之间有什么区别?

如果我想在方法的句柄中更改全局变量角色,apis,total当前更改,那么我必须保存上下文。

但是为什么我可以直接在创建中使用它?

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;
            });
        }
    });

如果你想在这些函数中使用this,你可以简单地使用箭头函数(ES6,不特定于 vuejs(:

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

阅读有关箭头函数的更多信息。

最新更新