我是 angular 11 的初学者,我想从 Mat-Dialog 获取数据,.open()
然后将其作为Goods 类数组的对象转换为另一个组件(ProductListComponent)
我的界面类:
export interface Goods {
goodsId?: number;
goodsCode?: string;
goodsName?: string;
goodsGroupId?: number | null;
goodsCateName?: string;
goodsGroupName?: string;
}
我将Array Object分配给dialogConfig.data
(起初 goodsSelect 变量为空,将在afterClosed()
调用时分配。)
export class PromotionAddComponent implements OnInit {
goodsSelect?: Goods[];
openDialog() {
const dialogConfig = new MatDialogConfig();
dialogConfig.data = this.goodsSelect;
const dialogRef = this.dialog.open(ProductListComponent, dialogConfig);
dialogRef.afterClosed().subscribe(data => {
this.goodsSelect = data;
});}}
从此组件中接收数据,MAT_DIALOG_DATA
但无法使用.filter()
.forEach()
或任何 Array 函数。
我试过Object.value()
但不工作,没有错误,也没有控制台日志显示。
并尝试过@Inject(MAT_DIALOG_DATA) private data: Goods[]
也不行。
export class ProductListComponent implements OnInit {
public allGoods?: Goods[];
public selectedItem?: Goods[];
displayedColumns: string[] = ['select', 'goodCateName', 'goodGroupName', 'goodCode', 'goodName', 'goodUnitName'];
dataSource = new MatTableDataSource<Goods>([]);
selection = new SelectionModel<Goods>(true, []);
constructor(
private goodsService: GoodsService,
private dialogRef: MatDialogRef<ProductListComponent>,
@Inject(MAT_DIALOG_DATA) private data: any) { }
ngOnInit(): void {
this.selectedItem = this.data as Goods[];
for(let item of this.selectedItem){
console.log(item.goodCode + " => " + item.goodName1);
}
this.dataSource.data.forEach(item => {
let res = this.selectedItem!.filter((e) => e.goodId == item.goodId);
if (res.length > 0) {
this.selection.select(item);
console.log("this.selectedItem was matched.");
}
});
}
}
尝试读取每个元素时显示此错误包括.map()
.forEach()
.for()
https://i.stack.imgur.com/EPlZU.png
console.log()
this.data 变量,有数据,但我无法读取或转换此对象。
我哪里弄错了?