从子组件调用函数时,组件属性不会更改

时间:2019-02-18 13:52:07

标签: javascript angular frontend angular7

当从子组件中调用方法时,在angular.io中父组件的Propos不会更改。

我在dashboard.component中有此方法:

listPeers(){
    this.loading = true;
    this.localStorage.getItem("payload").subscribe( (storage : string) =>{
      this.jwt = storage;
      this.userService.listPeers(this.jwt).subscribe( (res) => {
        this.loading = false;
        if(res.success){
          console.log(res.peers)
          this.peers = res.peers;
          this.listUsersCrm();
        }else{
          this.snackBar.openFromComponent(ToastComponent, {
            data: res.msg
          });
          this.router.navigate(['/notfound']);
        }
      })
    })
  }

在“ commit-changes.component”上,我称之为:

import { Component, OnInit} from '@angular/core';
import { DashboardComponent } from "../dashboard/dashboard.component";

@Component({
  providers: [DashboardComponent],
  selector: 'app-commit-changes',
  templateUrl: './commit-changes.component.html',
  styleUrls: ['./commit-changes.component.scss']
})
export class CommitChangesComponent implements OnInit {

  constructor(private dashBoardComponent : DashboardComponent) { }

  ngOnInit() {
  }

  discardChanges(){
    this.dashBoardComponent.listPeers();
  }


}

该方法已经被调用,但是来自父对象(dashboard.component)的道具没有改变。

注意:方法listPeers正在调用API以列出存储在BD上的对等项并设置对等项。

this.peers = res.peers;

执行此方法并应用道具更改的正确方法是什么?

0 个答案:

没有答案