如何将数据传递到 angular material bottomsheet

how Pass data to angular material bottomsheet

我用 Angular Material 和 Angular 6. 我经常使用 material 对话框,我是这样的:

openDialog3(key : string): void {
  let dialogRef = this.dialog.open(PPSDialogRemoveComponent, {width: '1000px'}); 
  dialogRef.componentInstance.key = key;
}

现在我想使用 angular materialbottomsheet。要将密钥传递给我的底部组件,我尝试这样做:

  openBottomSheet(key: string): void {
    let dialogRef = this.bottomSheet.open(BottomSheetOverviewExampleSheet);
    dialogRef.componentInstance.key = key;
}

但是我有这个错误

ERROR in src/app/geo/geo.component.ts(568,15): error TS2339: Property 'componen Instance' does not exist on type 'MatBottomSheetRef'.

感谢您的帮助

componentInstance 属性 将仅适用于使用 ComponentFactoryResolver 方法创建并使用 ViewContainerRef 插入 DOM 的动态组件。

根据 Angular Material 文档,模型弹出窗口的 dialog.open returns 引用。 https://material.angular.io/components/dialog/api

复制自 Angular Material:与底部 sheet 组件共享数据。

如果你想传递一些数据到底部sheet,你可以使用数据属性:

const bottomSheetRef = bottomSheet.open(HobbitSheet, {
  data: { names: ['Frodo', 'Bilbo'] },
});

之后您可以使用 MAT_BOTTOM_SHEET_DATA 注入令牌访问注入的数据:

import {Component, Inject} from '@angular/core';
import {MAT_BOTTOM_SHEET_DATA} from '@angular/material';

@Component({
  selector: 'hobbit-sheet',
  template: 'passed in {{ data.names }}',
})
export class HobbitSheet {
  constructor(@Inject(MAT_BOTTOM_SHEET_DATA) public data: any) { }
}

注意:Angular Material的版本是v7.0.3

看看这个: Sharing data with the bottom sheet component

这可能对某些人有用。我需要在底部 sheet 添加一个小的 table 以便扩展 @YenYees 回答:

import {Component, Inject} from '@angular/core';
import {MatBottomSheet, MatBottomSheetRef} from '@angular/material';
import {MAT_BOTTOM_SHEET_DATA} from '@angular/material';
@Component({
  selector: 'bottom-sheet-overview-example',
  templateUrl: 'bottom-sheet-overview-example.html',
  styleUrls: ['bottom-sheet-overview-example.css'],
})
export class BottomSheetOverviewExample {
  constructor(private bottomSheet: MatBottomSheet) {}

 ELEMENT_DATA: PeriodicElement[] = [
  {position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
  {position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'},
  {position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li'},
  {position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be'}
];

  openBottomSheet(): void {
    this.bottomSheet.open(BottomSheetOverviewExampleSheet, {
  data:  this.ELEMENT_DATA ,
});
  }
}

@Component({
  selector: 'bottom-sheet-overview-example-sheet',
  templateUrl: 'bottom-sheet-overview-example-sheet.html',
  styleUrls: ['bottom-sheet-overview-example-sheet.css'],
})
export class BottomSheetOverviewExampleSheet {
  constructor(@Inject(MAT_BOTTOM_SHEET_DATA) public data: any, private bottomSheetRef: MatBottomSheetRef<BottomSheetOverviewExampleSheet>, ) {}
  displayedColumns: string[] = ["position", "name", "weight", "symbol"];
  dataSource =  this.data;

  openLink(event: MouseEvent): void {
    this.bottomSheetRef.dismiss();
    event.preventDefault();
  }
}
export interface PeriodicElement {
  name: string;
  position: number;
  weight: number;
  symbol: string;
}

底部-sheet-概览示例-sheet.html 为

<mat-table [dataSource]="dataSource" class="mat-elevation-z8">
  <ng-container matColumnDef="position">
    <mat-header-cell *matHeaderCellDef> No. </mat-header-cell>
    <mat-cell *matCellDef="let element"> {{element.position}} </mat-cell>
  </ng-container>
  <ng-container matColumnDef="name">
    <mat-header-cell *matHeaderCellDef> Name </mat-header-cell>
    <mat-cell *matCellDef="let element"> {{element.name}} </mat-cell>
  </ng-container>
  <ng-container matColumnDef="weight">
    <mat-header-cell *matHeaderCellDef> Weight </mat-header-cell>
    <mat-cell *matCellDef="let element"> {{element.weight}} </mat-cell>
  </ng-container>
  <ng-container matColumnDef="symbol">
    <mat-header-cell *matHeaderCellDef> Symbol </mat-header-cell>
    <mat-cell *matCellDef="let element"> {{element.symbol}} </mat-cell>
  </ng-container>
  <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
  <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>

stackblitz