首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >同样的新闻-在Ionic 4上点击不同的新闻(纽西米)

同样的新闻-在Ionic 4上点击不同的新闻(纽西米)
EN

Stack Overflow用户
提问于 2019-03-08 10:50:12
回答 1查看 33关注 0票数 0

我在这里学习一个教程:https://www.youtube.com/watch?v=NJ9C7iY9350。无法理解为什么我要点击一个新闻,新闻细节总是一样的,我正在检查我的代码和它在github中的相同。但是不知道为什么不能看到每一个新闻的细节,只是我点击的第一条新闻。

附上我的代码:

新闻

TS

代码语言:javascript
复制
    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

代码语言:javascript
复制
    <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

代码语言:javascript
复制
export class NewsDetailPage implements OnInit {

article

constructor(private newsService: NewsService) { }

ngOnInit() {
    this.article = this.newsService.currentArticle;
    console.log(this.article)
}
}

tabs.router.module.ts

代码语言:javascript
复制
 ....
{ path: 'news', loadChildren:'../news/news.module#NewsPageModule' },
{ path: 'news-detail', loadChildren:'../news-detail/news-detail.module#NewsDetailPageModule' },
....
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-15 08:34:19

最后,我解决了这个问题:

代码语言:javascript
复制
ionViewWillEnter() {
    this.article = this.newsService.currentArticle;
    console.log(this.newsService.currentArticle)
}

每次当我点击一个新的“新闻”时,它都会改变。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55061616

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档