Angular 如何从组件重新渲染角度组件视图';是否在收到websocket消息后创建了s**ts**文件?

Angular 如何从组件重新渲染角度组件视图';是否在收到websocket消息后创建了s**ts**文件?,angular,websocket,vue-component,angular8,Angular,Websocket,Vue Component,Angular8,我有一个在ngOnInit()上初始化的用户列表,当我删除、添加或修改一个用户时,HTML组件会通过reloadData()函数重新加载。。但是,当我从WebScoket服务接收到消息并调用reloadData()函数时,它会从数据库而不是视图刷新模型 这是my EmployeesComponent.ts上的重载数据()函数: private reloadData() { this.users = []; console.log('reloading employees..'); console

我有一个在ngOnInit()上初始化的用户列表,当我删除、添加或修改一个用户时,HTML组件会通过reloadData()函数重新加载。。但是,当我从WebScoket服务接收到消息并调用reloadData()函数时,它会从数据库而不是视图刷新模型

这是my EmployeesComponent.ts上的重载数据()函数:

private reloadData() {
this.users = [];
console.log('reloading employees..');
console.log(this.users);
this.userService.list().subscribe(r => {
  if (this.router.url === '/RemoteMonitoring/(mainCon:Departments)' && this.clickedDep.depId !== -1) {
    this.chefDep = null;
    this.setChefDep(this.clickedDep.depId);
    this.usersForDep = r;
    for (const emp of this.usersForDep) {
      if (emp.department.depId === this.clickedDep.depId) {
        this.users.push(emp);
      }
    }
    this.clickedDep.users = [];
    this.clickedDep.users = this.users;
    this.outPutData.emit();
  } else {
    this.userService.findUserWithToken().subscribe(us => {
      for (const emp of r) {
        // @ts-ignore
        if (emp.userId !== us.userId) {
          this.users.push(emp);
        }
      }
    });
  }
});}
identify(index, item) {
return index;
reloadFromSocket() {
console.log('Reloading users from websocket...');
this.reloadData();
这是我显示用户的地方[]

 <tr *ngFor="let emp of users; let i = index; trackBy:identify">...</tr>
}

这是由websocket触发的函数,在它内部我调用reloadData():

private reloadData() {
this.users = [];
console.log('reloading employees..');
console.log(this.users);
this.userService.list().subscribe(r => {
  if (this.router.url === '/RemoteMonitoring/(mainCon:Departments)' && this.clickedDep.depId !== -1) {
    this.chefDep = null;
    this.setChefDep(this.clickedDep.depId);
    this.usersForDep = r;
    for (const emp of this.usersForDep) {
      if (emp.department.depId === this.clickedDep.depId) {
        this.users.push(emp);
      }
    }
    this.clickedDep.users = [];
    this.clickedDep.users = this.users;
    this.outPutData.emit();
  } else {
    this.userService.findUserWithToken().subscribe(us => {
      for (const emp of r) {
        // @ts-ignore
        if (emp.userId !== us.userId) {
          this.users.push(emp);
        }
      }
    });
  }
});}
identify(index, item) {
return index;
reloadFromSocket() {
console.log('Reloading users from websocket...');
this.reloadData();
}

我知道代码运行得很好,因为我正在控制台中记录所有内容。。您可以在这里看到,正在记录“从websocket重新加载用户…”消息。。

模型正在重新加载,但视图没有重新提交。


谢谢我发现了问题。。我将组件注入到服务中,因此它创建了一个新的实例。。因此,它会重新加载新实例的数据,但不会加载屏幕上呈现的数据,下面是解决该问题的解决方案:


我发现了问题。。我将组件注入到服务中,因此它创建了一个新的实例。。因此,它会重新加载新实例的数据,但不会加载屏幕上呈现的数据,下面是解决该问题的解决方案: