关于typescript:Angular 4 – 如何正确使用.find()?

Angular 4 - How to properly use .find()?

本问题已经有最佳答案,请猛点这里访问。

我已经阅读了大部分与此相似的问题,但我仍然不明白如何在我的应用中使.find()工作。

我有一个API服务:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
export class VehicleService {
  private defUrl = 'API';

  constructor(private http: Http) { }

  getVehicleByVehicleId(vehicleid?: any) {
    const url = (!vehicleid) ? this.defUrl : 'API1&vehicle_id=' + vehicleid;
    return this.http.get(url)
      .map(res => res.json());
  }


    searchVehicleId(description?: string) {
    const url = (!description) ? this.defUrl : 'API2&description=' + description;
    return this.http.get(url)
      .map(res => res.json().vehicles);
  }
}

零件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
export class VehicleComponent implements OnInit {

  ngOnInit() {

    this.searchQuery = new FormGroup({
      vehicleDescription: new FormControl('', Validators.required)
    });
  }


  constructor(private vehicleService: VehicleService) { }

  public fleet: Fleet[];
  public description: Description[];
  public searchQuery: FormGroup;
  public searchVehicleId: any;


  public searchByVehicleId(searchQuery) {
    this.vehicleService
      .searchVehicleId(searchQuery.value.vehicleDescription)
      .subscribe(searchQuery => {
        this.description = searchQuery;
        this.searchVehicleId = this.description.find() // <= HOW TO IMPLEMENT THIS?
    this.vehicleService
      .getVehicleByVehicleId(this.searchVehicleId)
      .subscribe(searchQuery => {
        this.vehicle = searchQuery;
      })
      });
  }
interface Vehicle {
  status: number;
  dallases: Vehicle[];
}

interface VehicleDetails {
  vehicle_id: number;
  dallassettings: string;
  dallasupdated: string;
  dallas_list: DallasList[];
}

interface DallasList {
  number: number;
  auth: number;
}

//////////////////////////////////
//////////////////////////////////

interface Description {
  vehicles: DescriptionDetails[];
}

interface DescriptionDetails {
  id: number;
  custom_description: string;
  description: string;
}

API2

1
2
3
4
5
6
7
8
9
10
11
{
   "vehicles": [{
       "description":"SKODA ROOMSTER",
       "id": 123456,
       "custom_description":""
    }, {
       "description":"SKODA ROOMSTER",
       "id": 123456789,
       "custom_description":""
    }]
}

所以,我在这里做的是:将FormGroup - vehicleDescription字段内的description传递给searchByVehicleId方法。 在这里,我想找到传递vehicleDescription velue的id(它是API2中的"description"字段)。 当我们找到id时,我们在this.vehicleService.getVehicleByVehicleId中使用它进一步...

主要问题是 - 我不知道如何正确实施.find()

谢谢


MDN文档:

1
this.searchVehicleId = this.description.find(x => x.id === someId);