尝试对返回的 Observable.subscribe() 数据数组进行排序... Angular 7 的新手。我对如何在这里使用 RXJS 方法、我需要在哪里导入什么以及如何对 Event 类型的数组进行排序感到困惑。
事件.ts
import {Venue} from './Venue';
export class Event {
id: number;
url: string;
date: string;
venue_id: number;
venue: Venue;
}
事件结果对象.ts
import {Event} from './Event';
export interface EventResultObj {
count: number;
next?: any;
previous?: any;
results: Event[];
}
事件服务.ts:
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Event } from '../models/Event';
import {Observable} from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class EventsService {
eventsUrl = 'http://localhost:4200/assets/tempData/events.json';
constructor( private http: HttpClient) { }
getEvents(): Observable<EventResultObj> {
return this.http.get<EventResultObj>(this.eventsUrl).pipe(
/// CANNOT FIGURE OUT HOW TO GET A SORT FUNCTION TO WORK HERE
/// WITH THE OBSERVABLE
)
}
}
事件列表.ts
import {Component, OnInit} from '@angular/core';
import {EventsService} from '../../services/events.service';
@Component({
selector: 'app-buyer-events-list',
templateUrl: './buyer-events-list.component.html',
styleUrls: ['./buyer-events-list.component.scss']
})
export class BuyerEventsListComponent implements OnInit {
constructor(private eventsService: EventsService) {
}
events;
ngOnInit() {
this.eventsService.getEvents().subscribe(events => this.events = events); // OR DO I ADD A PIPE OR MAP HERE AND HOW DO I DO IT?
}
}