Axios 和 VueJS,功能(响应)不设置列表
Axios and VueJS, function(response) is not setting a list
我有一个获取一些数据并将其添加到变量的请求,
当我使用:
.then(function(response) {
this.persons = response.data;
});
它不会将 response.data
分配给 this.persons
但是当我执行以下操作时:
.then(response => this.persons = response.data);
它指定它很好用。请看js fiddle:
正如我所解释的那样, arrow syntax does not bind it's own this, arguments, super, or new.target. Arrow functions are always anonymous。这些函数表达式最适合 non-method 函数。
this
的范围在 function() block and it does not refer 内更改为当前正在执行的函数,而对于箭头函数,this
仅指当前正在执行的函数。
我有一个获取一些数据并将其添加到变量的请求,
当我使用:
.then(function(response) {
this.persons = response.data;
});
它不会将 response.data
分配给 this.persons
但是当我执行以下操作时:
.then(response => this.persons = response.data);
它指定它很好用。请看js fiddle:
正如我所解释的那样
this
的范围在 function() block and it does not refer 内更改为当前正在执行的函数,而对于箭头函数,this
仅指当前正在执行的函数。