我正在尝试解析从 swapi 获得的 API 响应,我希望能够实现,但 API 响应仅出现在控制台日志中。我也想得到我制作的表格的回应。
swapi 是一个公共 API,您可以在这里找到https://swapi.co/
这是我的代码。车辆.html
<div class="card mb-3">
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0"
type="submit">Search</button>
</form>
</div>
<div class="card mb-3">
<div class="card-header"><b>Vehicle</b></div>
<div class="card-body table-responsive">
<table class="table table-hover table-striped">
<thead>
<tr>
<th>No</th>
<th>Name</th>
<th>Vehicle Class</th>
<th>Manufacture</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let veh of vehicle; let i = index">
<td>{{ i+1 }}</td>
<td>{{ veh?.name }}</td>
<td>{{ veh?.vehicle_class }}</td>
<td>{{ veh?.manufacturer }}</td>
</tr>
</tbody>
</table>
</div>
</div>
车辆.ts
import { Component, OnInit } from '@angular/core';
import { Vehicle } from './vehicle.model';
import { ApiService } from 'src/app/api.service';
@Component({
selector: 'app-vehicle',
templateUrl: './vehicle.component.html',
styleUrls: ['./vehicle.component.scss']
})
export class VehicleComponent implements OnInit {
vehicle: Vehicle[];
constructor(private apiService: ApiService) { }
rows : any;
temp : any;
applications: any;
ngOnInit() {
this.apiService.getVehicles()
.subscribe(data => {
this.applications = data
this.rows = data;
this.temp = data;
console.log(this.applications);
}
)
}
}
车辆模型.ts
export class Vehicle{
public name: string;
public model: string;
public manufacturer: string;
public cost_in_credits: string;
public length: string;
public max_atmosphering_speed: string;
public crew: string;
public passengers: string;
public cargo_capacity: string;
public consumables: string;
public vehicle_class: string;
}
郎朗坤
至尊宝的传说
HUX布斯
相关分类