我正在尝试将用户的ID发布到我的服务(addGrades),但我目前很困惑如何完成这项工作。我的第一次尝试是使用ActivateRoute从URL捕获id,但这不会有效。
//service
addGrades(user:any){
const body = JSON.stringify(user);
const headers = new Headers();
headers.append('Content-Type', 'application/json');
return this.http.post('http://localhost:9000/routes/grades/v1/course/'+user.id+'/student', body, {headers: headers})
.map((data:Response) => data.json());
}
//component.ts
export class Component implements OnInit{
private id;
constructor(private httpService: HttpService , private _route:ActivatedRoute) {}
ngOnInit() {
this._route.params.subscribe(params => {if (params['id']) {
this.id = params['id'];
}
})
}
Submit(id){
this.httpService.addGrades(this.model)
.subscribe(data => {
console.log(data);
})
}