我在这里学习一个教程:https://www.youtube.com/watch?v=NJ9C7iY9350。无法理解为什么我要点击一个新闻,新闻细节总是一样的,我正在检查我的代码和它在github中的相同。但是不知道为什么不能看到每一个新闻的细节,只是我点击的第一条新闻。
附上我的代码:
新闻
TS
goToNews(article) {
this.newsService.currentArticle = article;
console.log(article)
this.router.navigate(['/tabs/news-detail']);
}
getNews() {
this.newsService.getData(`top-headlines?countries=${this.selectedCountry}&category=technology`)
.subscribe(data => {
this.news = data;
console.log(this.selectedCountry)
})
}HTML
<ion-list *ngIf="!isResult">
<ion-item *ngFor="let country of countries" (click)="selectCountry(country)">
{{ country }}
</ion-item>
</ion-list>
<ion-card *ngFor="let article of news?.articles" (click)="goToNews(article)">
<ion-card-content>
<ion-card-title>{{ article.title }}</ion-card-title>
</ion-card-content>
</ion-card>新闻-详情
TS
export class NewsDetailPage implements OnInit {
article
constructor(private newsService: NewsService) { }
ngOnInit() {
this.article = this.newsService.currentArticle;
console.log(this.article)
}
}tabs.router.module.ts
....
{ path: 'news', loadChildren:'../news/news.module#NewsPageModule' },
{ path: 'news-detail', loadChildren:'../news-detail/news-detail.module#NewsDetailPageModule' },
....发布于 2019-03-15 08:34:19
最后,我解决了这个问题:
ionViewWillEnter() {
this.article = this.newsService.currentArticle;
console.log(this.newsService.currentArticle)
}每次当我点击一个新的“新闻”时,它都会改变。
https://stackoverflow.com/questions/55061616
复制相似问题