我想从表flight_id=1
中使用flight
打印记录数据,但是要从地址路径中获取此ID,如图片所示:
它有效,但是仅当我这样写this.flight_id = 1;
时,但我想从以下地址获得flight_id
:localhost:4200/myflight/1
这是我的桌子flight
:
我已在app.module.ts
我的课程myflights-list.component.ts
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import { FlightService } from '../flight.service';
import { Flight } from '../flight';
import {TicketService} from '../ticket.service';
import {ActivatedRoute, Router} from '@angular/router';
import {Ticket} from '../auth/jwt-response';
@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 ticketService: TicketService, private flightService: FlightService,
private router: Router, private route: ActivatedRoute) { }
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.reloadData();
}
reloadData() {
this.flightService.getFlight(this.flight_id).
subscribe((response) => {
this.flight = response;
});
}
}
flight.service.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}`);
}
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { MyflightsListComponent } from './myflights-list/myflights-list.component';
import { AppRoutingModule } from './app-routing.module';
import { HttpClientModule } from '@angular/common/http';
import {httpInterceptorProviders} from './auth/auth-interceptor';
@NgModule({
declarations: [
AppComponent,
MyflightsListComponent,
],
imports: [
BrowserModule,
FormsModule,
AppRoutingModule,
HttpClientModule
],
providers: [httpInterceptorProviders],
bootstrap: [AppComponent]
})
export class AppModule { }
app-routing.module.ts
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { MyflightsListComponent } from './myflights-list/myflights-list.component';
const routes: Routes = [
{ path: 'myflight/:flight_id', component: MyflightsListComponent },
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
答案 0 :(得分:3)
import { ActivatedRoute, Params } from '@angular/router';
constructor(private route: ActivatedRoute){ }
ngOnInit(){
this.route.params.subscribe((params: Params) => {
this.flight_id = params['flight_id'];
});
}
答案 1 :(得分:1)
这对我有用:
ave
答案 2 :(得分:0)
route.snapshot
是组件创建后不久的路线信息的静态图像。
您可以从paramMap
快照订阅activatedRoute
并获取参数值:
ngOnInit() {
this.flight_id = this.route.snapshot.paramMap.get('ticket_id');
}