简体   繁体   English

组件间传递 Angular

[英]Passing Between Components Angular

I currently have 3 components stacked like this:我目前有 3 个这样堆叠的组件:

<app-header></app-header>
<app-body></app-body>
<app-footer></app-footer>

However I want to switch it so that the footer and body positioning is different like below:但是我想切换它,以便页脚和正文位置不同,如下所示:

<app-header></app-header>
<app-footer></app-footer>
<app-body></app-body>

Once I change the placement of and the footer is no longer loading anything from the dataservice.一旦我更改了页脚的位置,页脚就不再从数据服务中加载任何内容。 Constructor inside footer.component.ts: footer.component.ts 内的构造函数:

constructor(private dataService: DataService) {
    //subscribing the the dataService so that we can display its contents
    this.dataService.currentNoms.subscribe(noms => {this.nominations.push(noms);});
  }

Constructor inside body.component.ts: body.component.ts 内的构造函数:

  constructor(private http: HttpClient, private dataService: DataService) { 
    //adding the nominations to my dataService to use between components
    this.dataService.addNominations(this.nominations);
  }

data.Service.ts:数据.Service.ts:

import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';

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

  private finalNoms = new BehaviorSubject<any>([]);
  currentNoms = this.finalNoms.asObservable();

  constructor() { 
    this.addNominations(this.nominations);
  }

  addNominations(nom: Object){
    this.finalNoms.next(nom);
  }

}

I tried swapping the contents of the constructor but that doesnt seem to be helping.我尝试交换构造函数的内容,但这似乎没有帮助。 Anyone know what im doing wrong?有谁知道我做错了什么? New to Angular, Thank you in advance! Angular 的新手,提前谢谢您!

It stands to reason that your BodyComponent is being loaded after your Footer component, and so this line of code has not been executed by the time your Footer component requests the data:理所当然的是,您的 BodyComponent 是在您的 Footer 组件之后加载的,因此当您的 Footer 组件请求数据时,这行代码尚未执行:

this.dataService.addNominations(this.nominations);

I would suggest moving the above line of code into the costructor of the Data Service, so that the data is always available, once the service becomes available.我建议将上面的代码行移动到数据服务的构造函数中,这样一旦服务可用,数据就始终可用。

Alternatively, you could leverage rxjs Subjects in your data service to asynchoronously set and retrieve the data:或者,您可以利用数据服务中的rxjs 主题来异步设置和检索数据:

// In data.service.ts
nominations = new BehaviorSubject([...]);
nominations$ = this.nominations.asObservable();

addNomination(nom) {
    const noms = this.nominations.value;
    noms.push(nom);
    this.nominations.next(noms);
}
// In body.component.ts
nominations: any[]; // Or the interface/type you're using

constructor(private dataService: DataService) {
    this.dataService.nominations$.subscribe(noms => this.nominations = noms);
}

addNomination(nom: any) {
   this.dataService.addNomination(nom);
}
// In footer.component.ts
nominations: any[];

constructor(private dataService: DataService) {
    this.dataService.nominations$.subscribe(noms => this.nomations = noms);
}

I think that you can achieve if you eclosed yours component in a div flex我认为如果你在 div flex 中封闭你的组件,你可以实现

<div [style]="display:flex;flex-direction: column"
     *ngIf="{order:orderService.currentNoms|async} as order" >
  <app-header [style.order]="order.order?order.order[0]:null"></app-header>
  <app-body [style.order]="order.order?order.order[1]:null"></app-body>
  <app-footer [style.order]="order.order?order.order[2]:null"></app-footer>
</div>

You main.cmponent你 main.cponent

  constructor(public orderService:OrderService){}

Your service您的服务

export class OrderService {

  private finalNoms = new BehaviorSubject<any>([]);
  currentNoms = this.finalNoms.asObservable();

  setOrder(order:any[])
  {
    this.finalNoms.next(order)
  }

Inside any component in ngOnInit在 ngOnInit 的任何组件中

 this.orderService.setOrder([1,3,2]) //change the order

A fool stackblitz 愚蠢的堆栈闪电战

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

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