我有一个请求来获取一些数据并将其添加到变量中,
当我使用:
.then(function(response) {
this.persons = response.data;
});
它不会response.data
分配给this.persons
而是当我执行以下操作时:
.then(response => this.persons = response.data);
它分配它很好用。请看js小提琴:
https://jsfiddle.net/trhhtyxr/2/
正如我在这里解释的那样,箭头语法不会绑定它自己的 this、arguments、super 或 new.target。箭头函数始终是匿名的。这些函数表达式最适合于非方法函数。
函数(( 块内部的this
范围更改,它不引用当前正在执行的函数,而对于箭头函数,this
仅引用当前正在执行的函数。