Axios 和 VueJS,function(response) 没有设置列表 [英] Axios and VueJS, function(response) is not setting a list
问题描述
我有一个请求,要获取一些数据并将其添加到一个变量中,
I have an request to get some data and add it to a variable,
当我使用时:
.then(function(response) {
this.persons = response.data;
});
它不会将 response.data
分配给 this.persons
但是当我执行以下操作时:
It does not assign response.data
to this.persons
but when I do the following:
.then(response => this.persons = response.data);
它可以很好地使用.请看js小提琴:
It assigns it fine to use. Please see the js fiddle:
https://jsfiddle.net/trhhtyxr/2/
推荐答案
正如我在这里、
As I have explained it here, arrow syntax does not bind it's own this, arguments, super, or new.target. Arrow functions are always anonymous. These function expressions are best suited for non-method functions.
this
的范围在 function() 块,它不引用当前正在执行的函数,而对于箭头函数,this
只引用当前正在执行的函数.
Scope of this
changes inside a function() block and it does not refer to the currently executing function, while with arrow function, this
refers to the currently executing function only.
这篇关于Axios 和 VueJS,function(response) 没有设置列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!