如何解决使用 ionic-http-plugin 无法在卡片上显示 api 的数据
我制作了一个小应用程序,想使用 ionic-http-plugin 在卡片上显示来自 news-api 的数据。但我无法在卡片上显示数据。有人可以告诉我缺少什么吗?我无法使用服务获取 home.page.ts 上的数据
新闻服务
import { HTTP } from '@ionic-native/http/ngx;
export class NewsService {
data: any;
constructor(private http: HTTP) { }
getData(url){
return (
this.http.get(`${API_URL}/${url}&apiKey=${API_KEY}`,{},{})
.then(res => {
this.data = res.data;
console.log(this.data);
})
.catch(error => {
console.log(error.error); // error message as string
})
)};
home.page.ts
export class HomePage {
data: any;
constructor(private newsService: NewsService) {
}
ngOnInit(){
this.data = this.newsService
.getData('top-headlines?country=in')
.then(res => {
console.log(res); //not getting data from news service
})
}
home.page.html
<ion-card *ngFor="let article of data?.articles" (click)="visitLink(article)">
<ion-img [src]="article.urlToImage"></ion-img>
<ion-card-header>
<ion-card-title>{{article.title}}</ion-card-title>
</ion-card-header>
<ion-card-content>
{{article.description}}
</ion-card-content>
</ion-card>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。