You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AngularJS重置密码时api_url读取报错:Cannot read properties of undefined

问题解决:Cannot read properties of undefined (reading 'api_url')

这个报错的核心原因是普通函数sendRequest内部的this没有指向类的实例,导致无法访问类的私有属性api_url。

修复方案

方案1:改用箭头函数定义sendRequest

箭头函数会继承外层作用域的this(也就是类实例的this),能直接访问api_url:

private api_url = AppSettings.API_ENDPOINT + 'user-info/email_validate';

// 改用箭头函数
private sendRequest = async <T>(email:string):Promise<T> => {
  const res = await fetch(this.api_url, {
    method:"post",
    headers: {"Content-Type": "application/json"},
    body: JSON.stringify({
      email: email,
    })
  });

  if(!res.ok){
    console.log("ERROR");
    // 可选:抛出错误让上层处理
    // throw new Error(`请求失败,状态码:${res.status}`);
  }

  return res.json();
}

同时优化了原代码中await和.then()混用的冗余写法,让逻辑更清晰。

方案2:调用时绑定this(不推荐,不如箭头函数简洁)

如果坚持用普通函数,调用时需要手动绑定类实例的this:

// 调用sendRequest的地方
this.sendRequest.bind(this)(userEmail)

内容的提问来源于stack exchange,提问作者Azamat25

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 01:05:56