无法分配为只读对象“ [object Object]”的“ closed”属性

时间:2019-04-09 07:54:57

标签: javascript angular

我已经为此努力了几天,但我找不到发生这种情况的原因。

所以我收到以下错误消息:

  

TypeError:无法分配为只读对象'[object Object]'的属性'closed'

enter image description here

如您所见,它发生在sendProposal函数的civilliability-step3.component.ts中:

civilliability-step3.component.ts

@Component({
  selector: 'app-civilliability-step3',
  templateUrl: './civilliability-step3.component.html',
  styleUrls: ['./civilliability-step3.component.scss']
})
export class CivilliabilityStep3Component implements OnInit, OnDestroy {
  @Output() modelChange = new EventEmitter<CivilLiabilityRequestType>();
  @Output() onCloseForm = new EventEmitter();
  @Input() model: CivilLiabilityRequestType;

  public formGroup: FormGroup;

  private closedProposalSub: Subscription;

  constructor(private formBuilder: FormBuilder, private store: Store) {}

  ngOnInit() {
    this.buildForm();

    if (this.model !== undefined && this.model.details.closed) {
      disableFormGroup(this.formGroup);
    }

    this.closedProposalSub = this.store
      .select(ProposalsState.closedProposalResult)
      .subscribe(val => {
        if (val !== undefined) {
          this.modelChange.emit(val);
          this.onCloseForm.emit();
        }
      });
  }

  ngOnDestroy() {
    if (this.closedProposalSub && !this.closedProposalSub.closed) {
      this.closedProposalSub.unsubscribe();
    }
    this.store.dispatch(new ResetClosedProposalResult());
  }  

  sendProposal() {
    this.model.details.closed = true;
    this.store.dispatch(new CloseProposal(this.model));
  }

  closeForm() {
    disableFormGroup(this.formGroup);
  }

  private buildForm() {
    this.formGroup = this.formBuilder.group({});
  }
}

组件的用途:

civilliability-detail.component.html

<app-civilliability-step3
          (onCloseForm)="step1.closeForm(); step2.closeForm(); step3.closeForm()"
          [(model)]="model"
          #step3></app-civilliability-step3>

我尝试以不同的方式分配真实值,因为我发现也许无法将其直接添加到模型中,该模型为Input。但这也无济于事。

  sendProposal() {
    const detailsModel = this.model.details;
    detailsModel.closed = true; // <-- same error
    this.model.details = detailsModel;

    const tmpModel = this.model;
    tmpModel.details.closed = true; // <-- same error
    this.model = tmpModel;

    // this.model.details.closed = true;
    this.store.dispatch(new CloseProposal(this.model));
  }

更新1:添加了CivilLiabilityRequestType

export interface CivilLiabilityRequestType extends IRequestData {
  details: CivilLiabilityDetailsModel;
  questionnaire: CivilLiabilityQuestionnaireModel;
  comments: CivilLiabilityCommentsModel;
}

export class CivilLiabilityDetailsModel {
  baseReqId: number;

  startDate: string;
  branch: NamedResource;
  fractioning: NamedResource;
  closed: boolean;
}

更新2:显示此模型的来源:

civilliability-detail.component.ts

export class CivilliabilityProposalDetailComponent implements OnInit, OnDestroy {
  @Input() model: CivilLiabilityRequestType;
  @Input() tab: Tab;
  @Input() tabs: Tab[] = [];
  @Input() selectedTabIndex;
  @Input() idx: number;

  constructor() {}

  ngOnInit() {}

  ngOnDestroy() {
    this.model = getEmptyCivilLiabilityRequest();
  }
}

detail.component.html

<mat-tab *ngFor="let tab of tabs; let idx = index">
...
<app-civilliability-proposal-detail
  [model]="tab.tabData.data"
  [tab]="tab"
  [tabs]="tabs"
  [selectedTabIndex]="selectedTabIndex"
  [idx]="idx"
>
</app-civilliability-proposal-detail>
...
</mat-tab>

detail.component.ts

@Component({
  selector: 'app-detail',
  templateUrl: './detail.component.html',
  styleUrls: ['./detail.component.scss']
})
export class DetailComponent implements OnInit {
  public tabs: Tab[] = [];
  public selectedTabIndex = 0;
  public quote?: QuoteData;
  public quoteModel: QuoteData;
  public originalModel: any[];
  public readonly = false;
  @Input() public requestType;

  constructor(private activeRoute: ActivatedRoute) {}

  ngOnInit() {
    const snapshot = this.activeRoute.snapshot;

    this.originalModel = snapshot.data['model'];
    if (this.originalModel) {
      this.tabs = this.createTabsFromQuoteModel(this.originalModel);
    }
  }

  private createTabsFromQuoteModel(model: any): Tab[] {
    let tabs: Tab[] = [];

    for (const key of Object.keys(model)) {
      const element = model[key];
      let type: RequestTypes;
      let proposalData: IRequestData = {};

      if (key === 'civilLiability') {
        type = RequestTypes.CivilLiability;
        proposalData.type = RequestTypes.CivilLiability;
        proposalData.data = element;
      }

      tabs = [...tabs, { type: type, name: '', tabData: proposalData }];
      proposalData = {};
    }

    return tabs;
  }
}

只是为了概述结构,以使我们保持理智:

<app-detail>
  <mat-tab *ngFor="let tab of tabs; let idx = index">
    <app-civilliability-proposal-detail [model]="tab.tabData.data">
      <app-civilliability-step3 [(model)]="model" ></app-civilliability-step3>
      </app-civilliability-step3
    </app-civilliability-proposal-detail>
  </mat-tab
</app-detail>

更新3::添加标签数据: enter image description here

2 个答案:

答案 0 :(得分:1)

我相信发生这种情况是因为您无法修改以状态(ngRx)存储的对象,而可以尝试执行以下操作:

sendProposal() {
  this.store.dispatch(new CloseProposal(Object.assign({}, this.model, {
    details: { closed: true }
  })));
}

答案 1 :(得分:0)

您必须使用Object.assign方法如下更改对象的值,

 Object.assign(target, source);