如何打印一个mysql记录的数据

rryofs0p  于 2021-06-15  发布在  Mysql
关注(0)|答案(2)|浏览(305)

我有一张table flight 在my mysql数据库中:

我只想在我的spring boot应用程序中显示第一条记录的数据 flight_id=1 这是我的json

myflights-list.component.html:

<h3>Ticket</h3>

<div *ngIf="flight">
<div>
<label>departureCity: </label> {{flight.departureCity}}
</div>
<div>
<label>destinationCity: </label> {{flight.destinationCity}}
</div>
<div>
<label>price: </label> {{flight.price}}
</div>
<div>
<label>flightDuration: </label> {{flight.flightDuration}}
</div>
<div>
<label>transferNumber: </label> {{flight.transferNumber}}
</div>
<div>
<label>departureDate: </label> {{flight.departureDate}}
</div>
<div>
<label>departureTime: </label> {{flight.departureTime}}
</div>

<hr/>
</div>

myflights-list.component.ts

@Component({
 selector: 'myflights-list',
 templateUrl: './myflights-list.component.html',
 styleUrls: ['./myflights-list.component.css']
})
export class MyflightsListComponent implements OnInit {

flight: Flight = new Flight();
flights: Observable<Flight>;
flight_id: number;
constructor(private flightService: FlightService,
          private router: Router, private route: ActivatedRoute, private token: TokenStorageService) { }

ngOnInit() {
  ngOnInit() {
 /*  this.flight_id = 1;*/
 this.route.params.subscribe(params => { this.flight_id = params['flight_id']; });
 this.flightService.getFlight(this.flight_id).subscribe(t => this.flight = t);
 this.flight_id = this.flight.flight_id;
 this.reloadData();
}
this.reloadData();
}

reloadData() {
this.flights = this.flightService.getFlight(this.flight_id);
}}

航班.ts

export class Flight {
flight_id: number;
departureCity: string;
destinationCity: string;
price: number;
flightDuration: number;
transferNumber: number;
departureDate: Date;
departureTime: Time;
tickets: Ticket[];
}
export class Ticket {
ticket_id: number;
place: number;
user_id: number;
flight_id: number;
}

航班服务.ts

import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Observable} from 'rxjs';
import {Flight} from './flight';

@Injectable({
providedIn: 'root'
})
export class FlightService {

private baseUrl = 'http://localhost:8080/api/flights';

constructor(private http: HttpClient) {
}

getFlight(flight_id: number): Observable<Flight> {
return this.http.get<Flight>(`${this.baseUrl}/${flight_id}`);
}

在我的春装 Boot 里,它看起来像是在照片上。不幸的是,我的航班没有数据 flight_id=1

jw5wzhpr

jw5wzhpr1#

两种方式:

是否必须直接订阅服务才能获取数据:

this.flightService.getFlight(this.flight_id).subscribe((response) => {

      this.flight = response;

    });
  }

或者,就像你打算用你的 flights 可观察,也可订阅:

reloadData() {

  this.flights = this.flightService.getFlight(this.flight_id);

  this.flights.subscribe((response) => {

     this.flight = response;

    });
  }}
}}
myzjeezk

myzjeezk2#

需要订阅 getFlight 方法。

reloadData() {
 this.flightService.getFlight(this.flight_id).subscribe((data) => {

   this.flights = data
  });
}}

相关问题