Axios 和 VueJS,function(response) 没有设置列表 [英] Axios and VueJS, function(response) is not setting a list

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

问题描述

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

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/

推荐答案

正如我在这里箭头语法 不绑定它自己的this, 参数supernew.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 的范围在 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屋!

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