Axios和VueJS,函数(响应)未设置列表 [英] 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/
推荐答案
如我所解释的那样此处,箭头语法不会绑定它自己的参数, new.target .箭头函数始终为匿名.这些函数表达式最适合非方法函数.
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
的范围在块,它未引用指向当前正在执行的功能,而带有箭头功能的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,函数(响应)未设置列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!