【问题标题】:Axios and VueJS, function(response) is not setting a listAxios 和 VueJS,函数(响应)没有设置列表
【发布时间】:2017-06-21 17:42:29
【问题描述】:

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

当我使用时:

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

它不会将response.data 分配给this.persons,但是当我执行以下操作时:

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

它可以很好地使用它。请看js小提琴:

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

【问题讨论】:

    标签: vuejs2 axios


    【解决方案1】:

    正如我所解释的 herearrow syntax 不绑定它自己的 thisargumentssupernew.target。箭头函数总是anonymous。这些函数表达式最适合非方法函数。

    this 的作用域在 function() 块内发生变化,does not refer 指向当前正在执行的函数,而对于箭头函数,this 仅指当前正在执行的函数。

    【讨论】:

      猜你喜欢
      • 2018-11-26
      • 1970-01-01
      • 2018-02-16
      • 1970-01-01
      • 2020-03-22
      • 2020-07-03
      • 2023-01-12
      • 2017-02-23
      • 1970-01-01
      相关资源
      最近更新 更多