1.
//服务中 //获取测验列表 getListData(): Promise<FormData[]>{ console.log(this.listUrl) return this.http.get(this.listUrl) .toPromise() .then(response => { console.log(response.json().data) return response.json().data as FormData[]; }) .catch(this.handleError) } //获取单个测验信息 getTest(id: number): Promise<FormData> { return this.getListData() .then(test => test.find(data => data.id == id)); }
2.编辑页面中
ngOnInit(): void { this.route.paramMap .switchMap((params: ParamMap) =>{ return this.dataService.getTest(+params.get('id')) }).subscribe((data)=>{ this.item = data; }); }
3.引入
import { ActivatedRoute, ParamMap } from '@angular/router'; // 构造 constructor( private dataService: DataService, private route: ActivatedRoute, private location: Location, ) { }