【问题标题】:How to Access JSON attribute in typeScript如何在 typeScript 中访问 JSON 属性
【发布时间】:2019-07-27 23:42:47
【问题描述】:

如何访问 typescript 中的 JSON 属性?

我正在制作 Angular 项目。
`

    test:string;
    response:any;

    web_assign() {
    this.http.get(this.url1).subscribe( e => this.response = e);
    this.test = "OK";
    this.t=this.response.name2;
    return false;}

它在这一行给出错误。 this.t=this.response.name2; 它说它无法读取'name2'的属性

【问题讨论】:

    标签: json typescript angular7


    【解决方案1】:

    您需要在订阅回调中进行赋值:

    this.http.get(this.url1)
        .subscribe( e => {
            this.response = e;
            this.test = "OK";
            this.t=this.response.name2;
        });
    

    http.get 是异步的,this.response 仍然不会在下一行定义,但会在 subscribe 回调中定义。

    【讨论】:

    • 根据他的声望分数,他不能接受,但这是我对你的赞成票XD
    猜你喜欢
    • 2013-10-17
    • 2020-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-02
    • 2021-10-20
    • 1970-01-01
    相关资源
    最近更新 更多