【问题标题】:Reading value from JSON从 JSON 中读取值
【发布时间】:2018-12-02 23:19:44
【问题描述】:

我有要打印的提示文件。每次按下按钮都会从 json 文件中打印下一个提示。 所以我写了一个函数,但现在从文件中读取并没有停止(附上日志的图像)。
有什么想法可以获取这些信息吗?
谢谢!

函数.ts

tips: any;

 getDataJSON() {
      return new Promise(resolve => {
        this.http.get('/assets/advices.json') 
          .subscribe((data) => {
            resolve(data);
            console.log(data);
           this.getDataJSON().then(data => {
              this.tips = data;
          }, err => {
            console.log(err);
          });
      });
    })
  }

html

<button ion-button block (click)="getDataJSON()">Fetch External Data</button>

【问题讨论】:

    标签: json angular http ionic-framework ionic3


    【解决方案1】:

    您正在进行不必要的嵌套调用

    getDataJSON() {
          return new Promise(resolve => {
            this.http.get('/assets/advices.json') 
              .subscribe((data) => {
                resolve(data);
                console.log(data);
              }, err => {
                console.log(err);
              });
          });
        })
      }
    

    【讨论】:

      猜你喜欢
      • 2016-10-12
      • 1970-01-01
      • 2020-12-06
      • 2017-03-27
      • 1970-01-01
      • 1970-01-01
      • 2013-03-06
      • 2017-03-02
      • 2019-03-11
      相关资源
      最近更新 更多