我在本地下载了News Feed with Images and Text Items Nativescript应用程序,并试图使其与newsapi.org的实时供稿一起使用。
原始应用具有json硬编码,如下所示:
this.msg
在我的allNews: { source: Source, author: string, title: string, description: string, url: string, urlToImage: string, publishedAt: string }[] = [{ "source": { "id": null, "name": "Yahoo.com" }, "author": null, "title": "The Latest: Russia says no evidence of gas attack in Douma", "description": null, "url": "https://www.yahoo.com/news/latest-turkey-urges-sides-avoid-more-syria-turmoil-113652213.html", "urlToImage": null, "publishedAt": "2018-04-13T19:44:00Z" }, { "source": { "id": "the-washington-post", "name": "The Washington Post" }, "author": "http://www.facebook.com/matt.zapotosky", "title": "Trump issues pardon to 'Scooter' Libby, former chief of staff to Vice President Cheney", "description": "The Bush administration aide was convicted of perjury before a grand jury, lying to FBI investigators and obstruction of justice.", "url": "https://www.washingtonpost.com/politics/trump-issues-pardon-to-scooter-libby-former-chief-of-staff-to-vice-president-cheney/2018/04/13/dfa4039a-3f2d-11e8-8d53-eba0ed2371cc_story.html", "urlToImage": "https://www.washingtonpost.com/rf/image_1484w/2010-2019/WashingtonPost/2018/04/13/National-Politics/Images/AFP_13Z4QQ.jpg?t=20170517", "publishedAt": "2018-04-13T19:06:25Z" }, { "source": { "id": "the-new-york-times", "name": "The New York Times" }, "author": "", "title": "Where's the Boom in Bank Lending?: DealBook Briefing", "description": "Bank lending was expected to surge this year. But going by bank results so far, lending in the first quarter is set to disappoint.", "url": "https://www.nytimes.com/2018/04/13/business/dealbook/trump-trans-pacific-partnership.html", "urlToImage": "https://static01.nyt.com/images/2018/02/03/us/14db-newsletter-wells/14db-newsletter-wells-facebookJumbo-v2.jpg", "publishedAt": "2018-04-13T18:56:00Z" }];
中添加:
app/home/home-view-model.ts
我正在根据https://docs.nativescript.org/ns-framework-modules/http此处的文档进行此操作。但是,出现错误:
import { getJSON } from "tns-core-modules/http";
...
allNews: { source: Source,
author: string,
title: string,
description: string,
url: string,
urlToImage: string,
publishedAt: string
}[] = getJSON("newsapi link").then((r: any) => {
}, (e) => {
});
;
任何帮助表示赞赏!
答案 0 :(得分:0)
您正在分配.then调用的结果,这是一个Promise。调用getJSON()和.then()都返回promise(以允许链接)。
相反,您想在promise的resolve处理程序内分配值:
allNews: { source: Source,
author: string,
title: string,
description: string,
url: string,
urlToImage: string,
publishedAt: string
}[]; // just declare the variable here
getJSON("newsapi link").then((r: any) => {
allNews = r; // assign it from the response when successful
}, (e) => {
});
以下是有关Promises的更多信息的链接,该信息可能会有所帮助: https://www.datchley.name/es6-promises/
答案 1 :(得分:0)
我想确定并在此处提供完整的解决方案。我需要将getJSON函数分配给变量才能使其正常工作。请注意下面的fooBar变量:
allNews: { source: Source,
author: string,
title: string,
description: string,
url: string,
urlToImage: string,
publishedAt: string
}[]; // just declare the variable here
getData = getJSON("newsapi link").then((r: any) => {
this.allNews = r; // assign it from the response when successful
}, (e) => {
});