首页 > 解决方案 > 无法从我的服务调用组件函数

问题描述

我正在使用 observables 从我的服务内部调用我的组件函数,这是我的服务代码:

  private trades = new Subject<any>();

  // Observable string streams
  tradeMethodCalled$ = this.trades.asObservable();

  constructor(private http:Http) { }

  getAllTrades() {
    let headers = new Headers();
    console.log('here');
    headers.append('Content-Type', 'application/json');
    this.http.get('http://localhost:50005/api/trading/trades',{
     headers: headers
   }).pipe(map((res : any)=>res.json()))
   .subscribe(
    (res) => {
     this.payload = res[0].state.data;
     console.log('success');
     console.log(this.payload);
     this.trades.next();
    }
   );
   }

这是我的组件代码

export class LandingPageComponent implements OnInit {
trades;
party;
  constructor(public service: CordaContentService,private routes : ActivatedRoute) { 
    this.service.tradeMethodCalled$.subscribe(
      () => {
        console.log(this.trades + "here");
        routes.params.subscribe(val => {
          this.trades=this.service.gettrades();
          console.log(this.trades);
    })
    });
  }

我究竟做错了什么?我的组件功能没有被触发,我正在使用 Angular 6。请提供一个明确的解决方案

标签: angulartypescriptangular-servicesangular-components

解决方案


推荐阅读