簡體   English   中英

如何以角度將數據從子組件傳遞到子組件?

[英]how to pass data from child to child component in angular?

deals-transaction.componentapp-transactions-details呈現,且deals-transaction.component呈現app-deals-approval component.

如何獲取 Deals-transaction.component testPage() 結果或輸出並將數據傳遞給app-deals-approval組件並訪問 app-deals-approval 組件 ngOnit 上的值?

@Viewchild 是一個好的解決方案嗎? 它應該作為輸入傳遞嗎? . 謝謝。

謝謝 。

#deals-transaction.component html(這會呈現 app-deals-approval)我想將數據傳遞給 app-deals-approval

  <div>
    <app-deals-approval > </app-deals-approval>
  </div>

#deals-transaction.component.ts

@Component({
  selector: 'app-deals-transaction',
  templateUrl: './deals-transaction.component.html',
  styleUrls: ['./deals-transaction.component.css'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class DealsTransactionComponent implements OnInit {
  @ViewChild(TableMultiSortComponent, { static: true }) tableMultiSortComponent: TableMultiSortComponent;
  transactionSubscripion: Subscription;
  tableOptions: any;
  @Input() transaction: any;
  isLoading: boolean;
  accountId: any;
  dealDetails: any;

  constructor(
    private _snackBar: MatSnackBar,
 
    private dealService: DealService,
 
  ) {}
  ngOnInit(): void {
    this.leaseId = this.transaction.leaseId;
    this.propertyId = this.transaction.propertyId;   
    const actions = ["Copy", "Delete", "For Approval"];
    this.testPage();
  }
  private testPage() {
    this.searchInput = '';
    const status = 'ForApproval'
    this.isLoading = true;
    this.dealService
      .getAllDeals(
        status,
        this.accountId,
        this.transaction.id,
        this.table.pageIndex + 1,
        this.table.pageSize,
        this.searchInput,
        this.table.sortParams,
        this.table.sortDirs
      )
      .pipe(finalize(() => (this.isLoading = false)))
      .subscribe((res) => {
        this.testPageOutput = res.items;
      }, (err) => this.notificationService.showError(err)
    );
  }

#deals-approval.component.ts 代碼

@Component({
  selector: 'app-deals-approval',
  templateUrl: './deals-approval.component.html',
  styleUrls: ['./deals-approval.component.css'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class DealsApprovalComponent implements OnInit {
  transactionSubscripion: Subscription;
  @ViewChild(TableMultiSortComponent, { static: true }) tableMultiSortComponent: TableMultiSortComponent;
  tableOptions: any;
  hasApproval = false;
  @Input() transaction: any;
  @Input() dataTest: any;
  isLoading: boolean;
  DEAL_TYPES = DEAL.TYPES;
  totalDeals : number;

  dealType$: Observable<string> = new Observable<string>()
  firstSub: Subscription;
  accountId: any;
  data: any;
  searchInput: string;
  table: any;
  dealType: string;
  totalDealsForApproval = 0;
  constructor(
    private dealService: DealService,
    private notificationService: NotificationService,
    private trasactionService: TransactionService,
    private route: Router,
    private dealTransactionService: DealTransactionService
    
  ) { }

  ngOnInit(): void {
 
 }

內部deals-transaction.component

<app-deals-approval [testPageOutput]="testPageOutput"> </app-deals-approval>

app-deals-approval component

// use set since your `testPage()` is generating result in async
_testPageOutput: any;
@Input() set testPageOutput(value: any) {
    this._testPageOutput = value;
    // other operation you want to perform upon `testPageOutput` value changes
}

這是因為testPageOutput由異步任務設置DealsTransactionComponent -這就是為什么它不在可用ngOnInit()DealsApprovalComponent 嘗試訪問它在ngOnChanges()代替ngOnInit()DealsApprovalComponent

@Component({
  selector: 'app-deals-transaction',
  templateUrl: './deals-transaction.component.html',
  styleUrls: ['./deals-transaction.component.css'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class DealsTransactionComponent implements OnInit, OnChanges {
  testPageOutput: any;

  constructor(
      private _snackBar: MatSnackBar,
      private dealService: DealService) {}

  ngOnInit(): void {
    this.testPage();
  }

  private testPage() {
    this.dealService.getAllDeals(/*...*/)
      .pipe(finalize(() => (this.isLoading = false)))
      .subscribe(
         (res) => {
           this.testPageOutput = res.items;
         }, 
         (err) => this.notificationService.showError(err)
      );
  }
}

模板:

<div>
  <app-deals-approval [testPageOutput]="testPageOutput"> 
  </app-deals-approval>
</div>

交易批准組件

@Component({
  selector: 'app-deals-approval',
  templateUrl: './deals-approval.component.html',
  styleUrls: ['./deals-approval.component.css'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class DealsApprovalComponent implements OnInit {

  @Input() testPageOutput: any;

  constructor() { 
  }

  ngOnInit(): void {
  }

  ngOnChanges(changes: any): void {
    if (changes.testPageOutput?.currentValue) {
      // do your stuff here
    }
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM