【问题标题】:compoent interaction with rxjs in angular组件与 rxjs 的 Angular 交互
【发布时间】:2021-02-06 14:22:21
【问题描述】:

我使用新闻 api 制作新闻应用程序,并且我正确地获取数据,但没有将数据从服务发送到组件,那么如何将数据从服务发送到组件
并通过 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);
     }  }

【问题讨论】:

    标签: node.js angular typescript


    【解决方案1】:

    这里是你没有导入 Observable,observable 这就是问题发生的原因

    从 '@angular/core' 导入 { Injectable };
    从 'rxjs' 导入 { Subject, Observable, observable };

    @Injectable()
    导出类 NewService {
    消息$:可观察的;
    私有 pushSource = new Subject();
    国家名称;

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

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

      this.pushSource.next(t_h);  
    }  
    

    【讨论】:

      猜你喜欢
      • 2017-07-18
      • 2020-09-15
      • 2017-08-26
      • 2020-06-01
      • 2017-04-21
      • 1970-01-01
      • 2020-09-17
      • 1970-01-01
      • 2023-04-04
      相关资源
      最近更新 更多