我在这里关注一个教程: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' },
....
最后我解决了它:
ionViewWillEnter() {
this.article = this.newsService.currentArticle;
console.log(this.newsService.currentArticle)
}
这样做总是随着我点击一个新的“新闻”而改变