我有3个选项卡,其中一个选项卡显示一个包含员工列表的表.第一次加载时工作正常.ngOnInit使用http get从服务器获取数据.之后,当我点击添加新员工打开一个表单,该表单从用户那里获取输入,当点击该提交时,我调用一个调用http post服务的函数将该数据发布到我的服务器,在那里它插入记录然后在那之后它被重定向回员工组件,但是现在已经加载了该员工组件,除非我重新编译我的代码,否则我看不到我在表中插入的新记录.
employee.component.ts(加载员工表)
import { Component, OnInit, OnDestroy } from '@angular/core';
import { EmployeeService } from '../employee.service';
@Component({
selector: 'app-employees',
templateUrl: './employees.component.html',
styleUrls: ['./employees.component.css']
})
export class EmployeesComponent implements OnInit {
public employeeObj:any[] = [{emp_id:'',empname:'',joindate:'',salary:''}] ;
constructor(private employeService:EmployeeService) { }
ngOnInit() {
this.employeService.getEmployees().subscribe(res => this.employeeObj = res);
}
}
Run Code Online (Sandbox Code Playgroud)
form.component.ts
import { Component, OnInit, OnDestroy } from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';
import { EmployeeService } from '../../employee.service';
import { Router } from '@angular/router';
@Component({
selector: …Run Code Online (Sandbox Code Playgroud)