我正在使用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);
})
});
}
我在做什么错? 我的组件功能没有被触发,我正在使用angular6。请提供清晰的解决方案