Axios和VueJS,函数(响应)未设置列表 [英] Axios and VueJS, function(response) is not setting a list

查看:121
本文介绍了Axios和VueJS,函数(响应)未设置列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我请求获取一些数据并将其添加到变量中,

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的范围在块,它未引用指向当前正在执行的功能,而带有箭头功能的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆