Vuejs2 此方法与创建的方法之间有什么不同?

Vuejs2 此方法与创建的方法之间有什么不同?,vuejs2,Vuejs2,这个变量在方法和created之间有什么区别 如果我想在方法的handleCurrentChange中更改全局变量personas、api、total,那么我必须保存上下文 但是为什么我可以直接在created中使用它呢 var vue = new Vue({ el: "#app", personas: {}, apis: {}, total: '', methods: { submitFor

这个变量在方法和created之间有什么区别

如果我想在方法的handleCurrentChange中更改全局变量personas、api、total,那么我必须保存上下文

但是为什么我可以直接在created中使用它呢

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