使用angular

时间:2018-12-03 21:51:24

标签: angular angular5 angular6

我有一个学生组件,可在加载页面上显示学生列表。我创建了另一个添加学生的组件,我想当我单击“自动提交”时,该学生组件将被更改并显示新学生而无需刷新页面。为此,在添加学生后,我从服务中调用函数onGetStudents(),向您展示了我在下面实现的解决方案,但它不起作用!

@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。

0 个答案:

没有答案