本文介绍了与angular中的rxjs的组件交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用新闻api创建新闻应用程序,并且我正确地获取了数据,但没有将数据从服务发送到组件,因此如何将数据从服务发送到组件
和组件通过rxjs到组件

i make news application with news api and i fetch data properly and but not sending data from service to component so how can send data from service to component
and component to component through rxjs

 import { Injectable } from '@angular/core';
 import { Subject } from 'rxjs';


 @Injectable()
 export class NewService {

     private pushSource = new Subject<object>();
       Country_Name;

     constructor(private http:HttpClient,) {
       this.messages$ = this.pushSource.asObservable()
       }    // define function for call the category
     categoryNews(category)
     {
       var categoryData = this.http.get(`https://newsapi.org/v2/top-headlines?country=in&category=${category}&apiKey=********`)
          .subscribe(Data=> this.pushSource.next(Data));
     }

  news(t_h)
     {
       console.log(t_h);
       this.pushSource.next(t_h);
     }  }

推荐答案

在这里您不导入Observable,这是问题发生的原因

here is you not import Observable, observable that's why the problem occurred

@Injectable()
导出类NewService {
messages $:可观察;
私人pushSource = new Subject();
国家名;

@Injectable()
export class NewService {
messages$: Observable;
private pushSource = new Subject();
Country_Name;

constructor(private http:HttpClient,) {
  this.messages$ = this.pushSource.asObservable()

}

categoryNews(category)
{
var categoryData = this.http.get( https://newsapi.org/v2/top-headlines?country=in&category=$ {category}& apiKey = ***** )
categoryData.subscribe(Data => this.pushSource.next(Data));
}
新闻(t_h)
{
console.log(t_h);

categoryNews(category)
{
var categoryData = this.http.get(https://newsapi.org/v2/top-headlines?country=in&category=${category}&apiKey=*****)
categoryData.subscribe(Data=> this.pushSource.next(Data));
}
news(t_h)
{
console.log(t_h);

  this.pushSource.next(t_h);
}

这篇关于与angular中的rxjs的组件交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 04:35