Axios and VueJS, function(response) is not setting a list

孤街醉人 提交于 2019-12-28 03:14:07

问题


I have an request to get some data and add it to a variable,

When I use:

.then(function(response) {
    this.persons = response.data;
});

It does not assign response.data to this.persons but when I do the following:

.then(response => this.persons = response.data);

It assigns it fine to use. Please see the js fiddle:

https://jsfiddle.net/trhhtyxr/2/


回答1:


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.

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.



来源:https://stackoverflow.com/questions/42039714/axios-and-vuejs-functionresponse-is-not-setting-a-list

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!