简体   繁体   English

在 Angular 中的组件之间共享数据

[英]Share data between components In Angular

Other posts don't have help me to solve this problem其他帖子没有帮助我解决这个问题

I want to share a variable retrieved from an HTTP request through two components and using a service.我想通过两个组件并使用服务共享从 HTTP 请求中检索到的变量。

For this I realized this, but unfortunately the data is not displayed in my components (no error in the console) nor compilation.为此我意识到了这一点,但不幸的是,我的组件中没有显示数据(控制台中没有错误),也没有编译。 sharedata.service.ts共享数据服务.ts

@Injectable({
  providedIn: 'root'
})
export class SharedataService {

  private dataSourceMyContainers = new Subject<any>();

  constructor(private dataService: RestApiService) { 
    this.getContainers();
  }

  getContainers(): any{
    this.dataService.getAllContainers().subscribe((res)=>{
      this.dataSourceMyContainers = res;       
    });    
  }

  getList(){
    return this.dataSourceMyContainers.asObservable();
  }
  
  updateApprovalMessage(message: any) {
    this.dataSourceMyContainers.next(message)
  }
}

First service for example status-card.component.ts第一个服务,例如 status-card.component.ts

@Injectable({
  providedIn: 'root'
})
export class StatusCardComponent implements OnInit {
  runningContainer?: number = 0;
  pausedContainer?: number = 0;
  stoppedContainer?: number = 0;
  failedContainer?: number = 0;
  dataSourceMyContainers: any = [];

  constructor(private sharedataService: SharedataService, private notification: NotificationsService) { 
  }

  ngOnInit(): any{
// it enters here
    this.sharedataService.getList().subscribe((res)=>{
      this.dataSourceMyContainers = res; // but not enter here
      console.log("res"+ res); // nothing displayed
    });    
  }  

  btnCountStatus(): void {
    this.runningContainer = this.dataSourceMyContainers.filter((container: any) => container.status === 'RUNNING').length;
    this.pausedContainer = this.dataSourceMyContainers.filter((container: any) => container.status === 'PAUSED').length;
    this.stoppedContainer = this.dataSourceMyContainers.filter((container: any) => container.status === 'STOPPED').length;
    this.failedContainer = this.dataSourceMyContainers.filter((container: any) => container.status === 'FAILED').length;
  }
}

Thanks for help !感谢帮助 !

There are two issues with your code:您的代码有两个问题:

  1. You need a BehaviorSubject instead of a Subject because u need the "replayed" value:您需要 BehaviorSubject 而不是 Subject 因为您需要“重播”值:

    private dataSourceMyContainers = new BehaviorSubject();私有 dataSourceMyContainers = new BehaviorSubject();

  2. You need to "next" your results form the Http-Call您需要从 Http-Call 中“下一个”您的结果

    getContainers(): any{ this.dataService.getAllContainers().subscribe((res)=>{ this.dataSourceMyContainers.next(res); getContainers(): any{ this.dataService.getAllContainers().subscribe((res)=>{ this.dataSourceMyContainers.next(res);
    }); });
    } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM