gpt4 book ai didi

javascript - this. 在 Angular2 的错误函数内部不可用

转载 作者:行者123 更新时间:2023-11-28 14:59:08 25 4
gpt4 key购买 nike

我在 angular2 中遇到了一个奇怪的错误。而下面的代码工作正常

loginResult.subscribe(
(data) =>
this.response = data,
(err) =>

this._ajaxService.handleError(err, 'A string to summarize the activity')
);

以下代码表示无法读取未定义的属性handleError。仅当请求失败时才会发生这种情况。

    loginResult.subscribe(
function (response) {

this.response = response;
console.log(this.response);

},
function (error) {

this._ajaxService.handleError(error, 'A string to summarize the activity')

}
);

最佳答案

“this”绑定(bind)到成功和失败函数。

loginResult.subscribe(
function (response) {
this.response = response;
console.log(this.response);
}.bind(this), // <== "this" binding here
function (error) {
this._ajaxService.handleError(error, 'A string to summarize the activity')
}.bind(this)
);

关于javascript - this.<servicename> 在 Angular2 的错误函数内部不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41738962/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com