【问题标题】:how to get json data in export const LANDMARKS : Landmark[] = this.myobj; using nativscript如何在 export const LANDMARKS 中获取 json 数据:Landmark[] = this.myobj;使用原生脚本
【发布时间】:2018-07-08 15:16:13
【问题描述】:

我正在调用一个 API,它给了我一个 JSON 数组,所以我想将返回的数据分配给 LANDMARKS 变量。

源代码:

export const LANDMARKS : Landmark[] = this.myobj;
   console.log(this.myItems);


 fetch('https://jsonplaceholder.typicode.com/users')
    .then(response => response.json())
    .then(json => {this.myItems =  json;

  console.log(this.myItems);
        **const myobj = this.myItems**
  // return myobj;
}

  );

【问题讨论】:

  • 所以您想将this.myItems 分配给Landmark[]?
  • Yes this.myItems to Landmark[]
  • 如果您从 API 获得响应,请尝试使用:LANDMARKS = this.myItems
  • 是的,我很喜欢 .then(json => {this.myItems = json; console.log(this.myItems);在此处输入代码 const myobj = this.myItems 数据在 Landmark[] 中正确显示听到但不显示
  • 请格式化代码并删除注释代码

标签: angularjs json nativescript


【解决方案1】:

试试下面的代码:

export const LANDMARKS: Landmark[] = [];

fetch('https://jsonplaceholder.typicode.com/users')
    .then(response => response.json())
    .then(json => {
        var myItems = json;
        this.LANDMARKS = myItems;
        console.log('--Landmark List--',this.LANDMARKS);
    });

【讨论】:

  • 不工作控制台日志文件:///app/mock-landmarks.js:168:12:未定义
  • 尝试控制台json的值?它是什么?
  • console.log('--地标列表--',this.LANDMARKS);在这个控制台中的值可以适当地增加
  • 好吧?这就是你需要的?
  • export const LANDMARKS: Landmark[] = []; // 如果我在此控制台中打印数据,则无法获取 console.log(this.LANDMARKS); fetch('jsonplaceholder.typicode.com/users') .then(response => response.json())
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-22
  • 2023-03-10
  • 1970-01-01
  • 2019-11-16
  • 1970-01-01
  • 1970-01-01
  • 2017-12-01
相关资源
最近更新 更多