我有一个请求获取一些数据并将其添加到变量中,
当我使用时:
.then(function(response) {
this.persons = response.data;
});
它不分配response.data
to this.persons
但是当我执行以下操作时:
.then(response => this.persons = response.data);
它分配它很好用。请参阅 js 小提琴:
https://jsfiddle.net/trhhtyxr/2/ https://jsfiddle.net/trhhtyxr/2/
正如我所解释的here https://stackoverflow.com/a/41960694/1610034, 箭头语法 https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions不绑定自己的this https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this, 论点 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments, super https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/super, or 新目标 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new.target。箭头函数总是匿名的 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name。这些函数表达式最适合非方法函数。
范围this
a 内的变化功能() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/function块和它不指 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions到当前正在执行的函数,而使用箭头函数,this
仅指当前正在执行的函数。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)