未从服务获取更新的数据到我的component1 onclick事件,在component2中以角度显示

问题描述 投票:1回答:2

我正在研究angular 9以进行新闻应用。我正在使用印度时报RSS Feed API https://timesofindia.indiatimes.com

这是我的stackblitz链接https://stackblitz.com/github/mehulk05/Newsapp

我的家庭组件中有类别,因此,当我单击任何类别时,我会在服务中获取该类别的新闻,并尝试在NewsList Component中显示它。但是当我更改类别时,无法以某种方式发送从服务到newsList Component的最新数据。尽管我正在NewsService的控制台中获取更新的数据,但无法在newsListComponent中获取该数据]

这是我的代码

Home.component.html

<div class="container mt-5">
    <div class="col-xs-12">


        <div class="row mt-4">
            <div class="col-xs-3 col-md-3 sidelink">
                <div class="list-group">
                    <a *ngFor="let c of categories" 
                    class="list-group-item list-group-item-action"
                     id="list-home-list" [routerLink]="" 
                     role="tab" aria-controls="home" 
(                    (click)='onClick(c.url)'>{{c.type}}</a>
                </div>
            </div>
            <div class="col-md-9 col-xs-9">
                <router-outlet></router-outlet>
            </div>
        </div>
    </div>
</div>

Home.component.ts

export class HomeComponent implements OnInit {

  categories=[
    {type:"home",url:"https://timesofindia.indiatimes.com/rssfeedstopstories.cms?feedtype=sjson"},
  {type:"recent",url:"https://timesofindia.indiatimes.com/rssfeeds/1221656.cms?feedtype=sjson"},
  {type:"india",url:"https://timesofindia.indiatimes.com/rssfeeds/-2128936835.cms?feedtype=sjson"},
  {type:"cricket",url:"https://timesofindia.indiatimes.com/rssfeeds/4719161.cms?feedtype=sjson"}]


    constructor(private ns:NewsserviceService,
      private router:Router) { }

    ngOnInit(): void {
    }

    onClick(title){
     console.log(title)
      this.ns.getUrl(title)
      this.router.navigate(["/news-list"])
    }

  }

NewsService.ts

export class NewsserviceService {
  url:string='https://timesofindia.indiatimes.com/rssfeedstopstories.cms?feedtype=sjson'
  constructor(private http:HttpClient) { 

  }

  getUrl(url){
    this.url=url
    this.getNewsList();
  }

  getNewsList(){
    this.url='https://cors-anywhere.herokuapp.com/'+this.url
  console.log(this.url)
     this.http.get(this.url ).subscribe(data=>{
      console.log(data)
    })
  }
}

NewsList.component.ts

news:any
    constructor(private ns:NewsserviceService,private cd:ChangeDetectorRef) { }

      ngOnInit() {
      this.news= this.ns.getNewsList();
      console.log("news is"  +this.news)
    }
}

当我单击任何类别时,我将首次从Newslist组件中获取控制台中的数据,但是之后我没有从newsList组件中获取任何数据,但是从Newservice中获得了控制台中的数据

angular angular6 angular7 angular8 angular-services
2个回答
1
投票

在这里我可以看到您想要做的是

  1. 来自组件1的Click事件,并向服务发送一些URL
  2. 在服务中获取该URL的数据
  3. 将该数据发送到组件2

现在您可以做的是,您可以在服务中创建一个事件发射器,并将事件与服务数据一起从组件1发射到组件2,因此如何执行此操作

Service.ts

 SelectedUrlData=new EventEmitter();

 getUrl(url){
    this.url=url
    this.getNewsList();

  }

  getNewsList() {
    this.url = 'https://cors-anywhere.herokuapp.com/' + this.url

    this.http.get<any[]>(this.url).subscribe(data => {
      this.tohome = data

    })

  }

  getData(): Observable<any> {
    this.url='https://cors-anywhere.herokuapp.com/'+this.url
  return this.http.get<any[]>(this.url);
}

Home.ts

@Input() newdata:any


    ngOnInit(): void {
      this.ns.getData().subscribe(data=>
        {
          console.log(data)

        })

    }

    onClick(title){

      this.ns.getUrl(title) 
      this.tohome=this.ns.tohome
      this.ns.selectedurl.emit(this.tohome)
      this.router.navigate(["/news-list"])
    }

newslist.ts

news:any
 ngOnInit() {
        this.ns.getData().subscribe(data=>
          {
            console.log(data)
          })
        this.news = this.ns.getNewsList();
        this.ns.selectedurl.subscribe(data => {
          console.log(data)
      })

    }

0
投票

仅获取对ViewChild的引用,然后直接调用其方法之一。

@Component({
 selector: 'app-child'
})
export class ChildComponent {

 notifyMe() {
 console.log('Event Fired');
 }
}

@Component({
 selector: 'app-parent',
 template: `<app-child #child></app-child>`
})
export class ParentComponent {

@ViewChild('child')
private child: ChildComponent;

ngOnInit() {
this.child.notifyMe();
}
}

或使用eventEmmiter。选中此post


0
投票

在这里我可以看到您想要做的是

  1. 来自组件1的Click事件,并向服务发送一些URL
  2. 在服务中获取该URL的数据
  3. 将该数据发送到组件2

现在您可以做的是,您可以在服务中创建一个事件发射器,并将事件与服务数据一起从组件1发射到组件2,因此如何执行此操作

Service.ts

 SelectedUrlData=new EventEmitter();

getNewsList(){
    this.url='https://cors-anywhere.herokuapp.com/'+this.url
      this.http.get<any[]>(this.url).subscribe(data=>{
      this.tohome=data

      console.log(this.tohome)    })

  }

Home.ts

@Input() newdata:any

 onClick(title){



      this.ns.getUrl(title)

      this.ns.getNewsList();   // this is done to invoke the newsservice everytime when a click url changes 
      this.newdata=this.ns.tohome
      console.log(this.newdata)

     this.ns.selectedurl.emit(this.newdata)  // emiting event with data you get from service

newslist.ts

news:any
ngOnInit() {

      this.ns.selectedurl.subscribe(data=>{
        console.log(data)
      })
© www.soinside.com 2019 - 2024. All rights reserved.