1
在我的Angular程序中,我需要創建對象的深層副本,以便我能夠比較這些更改。這是我試過的,我的控制檯出現ERROR TypeError: .forEach is not a function
錯誤。我該如何解決?錯誤類型錯誤:.forEach不是函數
import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { PTOData } from './pto-data';
import { PTODataService } from './pto-data.service';
import { EmpInfo } from './emp-info';
import { EmpInfoService } from './emp-info.service';
@Component({
selector: '[pto-row-edit]',
templateUrl: `./row-edit.component.html`,
styleUrls: ['./row-edit.component.css']
})
export class RowEditComponent implements OnInit {
// Inputs & Outputs to/from grid component
@Input() pto: PTOData[];
@Input() rowSelected: number;
@Output() onDelete = new EventEmitter<number>();
@Output() onSave = new EventEmitter<number>();
opto: PTOData[];
private initialized: boolean = false;
ngOnInit(): void {
this.opto = [];
this.pto.forEach(x => this.opto.push(x))
console.log(this.opto);
}
constructor(
private ptodataService: PTODataService,
private empInfoService: EmpInfoService) { }
resetRow(pto: PTOData): void {
console.log(this.pto);
console.log(this.opto);
this.rowSelected = null;
this.onSave.emit(this.rowSelected);
}
}
它看起來應該工作。這個組件的輸入可能不實際發送數組嗎? – SaxyPandaBear
@SaxyPandaBear我想這可以解決這個問題。每當我做一個基本的'console.log(this.pto)'它顯示它並且說它是一個對象。我將如何複製這樣的東西,但對於一個對象? – asdf
如果您在Chrome或Firefox等瀏覽器上使用開發人員工具,您是否可以看到該對象的內容?它是否包含一個數組? – SaxyPandaBear