@Injectable()
export class myservice {
constructor(private httpClient:HttpClient){}
onGetStudents(){
return this.httpClient.get("http://localhost:8080/students");
}
}
export class StudentsComponent implements OnInit{
constructor(private httpClient:HttpClient,private myservice:myservice) { }
ngOnInit() {
this.getStudents();
}
getStudents(){
this.myservice.onGetStudents()
.subscribe(data=>{
this.listStudents=data;
},err=>{
console.log(err);
});
}
}
export class addStudentComponent implements OnInit {
student:any;
constructor(private httpClient:HttpClient,private myservice:myservice) { }
ngOnInit() {
}
onSaveStudent(student){
this.httpClient.post("http://localhost:8080/students",students)
.subscribe(data=>{
this.student=data;
this.myservice.onGetStudents();
},err=>{
console.log(err);
})
}
}
PS:我当然在提供商中添加了myservice。