属性 ‘id’ 在传递数据时在类型 ‘{}’ 上不存在

huangapple go评论62阅读模式
英文:

Property 'id' does not exist on type '{}' when passing data

问题

以下是翻译好的部分:

在传递数据时,我遇到了以下错误:

属性 'id' 在类型 '{}' 上不存在。

以下是我的代码:

Service

import { Injectable } from '@angular/core';
import { BehaviorSubject, Observable, Subject } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class SidePanelAccordionService {
  private _openStateBehaviorSubject = new BehaviorSubject({});
  public openState$ = this._openStateBehaviorSubject.asObservable();

  public setOpenState(data: { id: string, state: boolean }) {
    this._openStateBehaviorSubject.next(data);
  }
}

TS

import { Component, Input, OnInit } from '@angular/core';
import { SidePanelAccordionData } from './side-panel-accordion.model';
import { SidePanelAccordionService } from './side-panel-accordion.service';

@Component({
  selector: 'app-side-panel-accordion',
  templateUrl: './side-panel-accordion.component.html',
  styleUrls: ['./side-panel-accordion.component.scss']
})
export class SidePanelAccordionComponent implements OnInit {
  @Input() data: SidePanelAccordionData;
  panelOpenState;

  constructor(private accordionStateService: SidePanelAccordionService) { }

  ngOnInit(): void {
  }

  stateValue() {
    this.data.panelOpenState = !this.data.panelOpenState;
    this.accordionStateService.setOpenState({ id: this.data.id, state: this.data.panelOpenState });
  }
}

Model

export class SidePanelAccordionData {
  public id?: string = '';
  public title: string = '';
  public panelOpenState: boolean;
  public expanded: boolean;

  constructor(args) {
    this.id = args.id;
    this.title = args.title;
    this.panelOpenState = args.panelOpenState;
    this.expanded = args.expanded;
  }
}

HTML

<mat-accordion>
    <mat-expansion-panel [id]="data.id" [opened]="data.panelOpenState === true" [closed]="data.panelOpenState === false" [expanded]="data.expanded" (click)="stateValue()">
        <mat-expansion-panel-header>
            <mat-panel-title>
                {{ data?.title }}
            </mat-panel-title>
        </mat-expansion-panel-header>
        <ng-content></ng-content>
    </mat-expansion-panel>
</mat-accordion>

引发错误的组件

this.accordionStateService.openState$.subscribe(data => {
  if (data.id === 'firstAccordionId') {
    this.firstDriverExpanded = true;
  } else if (data.id === 'secondAccordionId') {
    this.secondDriverExpanded = false;
  } else if (data.id === 'thirdAccordionId') {
    this.thirdDriverExpanded = false;
  }
});

如果我console.log(data),我会得到{}。在订阅之前如何设置对象中的值?

英文:

I am getting the following error when passing data

Property &#39;id&#39; does not exist on type &#39;{}&#39;.

Here is my code

Service

import { Injectable } from &#39;@angular/core&#39;;
import { BehaviorSubject, Observable, Subject } from &#39;rxjs&#39;;

@Injectable({
  providedIn: &#39;root&#39;
})
export class SidePanelAccordianService {
  private _openStateBehaviorSubject = new BehaviorSubject({});
  public openState$ = this._openStateBehaviorSubject.asObservable();

  public setOpenState(data: { id: string, state: boolean }) {
    this._openStateBehaviorSubject.next(data);
  }
}

TS

import { Component, Input, OnInit } from &#39;@angular/core&#39;;
import { SidePanelAccordianData } from &#39;./side-panel-accordian.model&#39;;
import { SidePanelAccordianService } from &#39;./side-panel-accordian.service&#39;;

@Component({
  selector: &#39;app-side-panel-accordian&#39;,
  templateUrl: &#39;./side-panel-accordian.component.html&#39;,
  styleUrls: [&#39;./side-panel-accordian.component.scss&#39;]
})
export class SidePanelAccordianComponent implements OnInit {
  @Input() data: SidePanelAccordianData;
  panelOpenState;

  constructor(private accordianStateService: SidePanelAccordianService) { }

  ngOnInit(): void {
  }

  stateValue() {
    this.data.panelOpenState = !this.data.panelOpenState;
    this.accordianStateService.setOpenState({ id: this.data.id, state: this.data.panelOpenState });
  }

}

Model

export class SidePanelAccordianData {
  public id?: string = &#39;&#39;;
  public title: string = &#39;&#39;;
  public panelOpenState: boolean;
  public expanded: boolean;

  constructor(args) {
    this.id = args.id;
    this.title = args.title;
    this.panelOpenState = args.panelOpenState;
    this.expanded = args.expanded;
  }
}

HTML

&lt;mat-accordion&gt;
    &lt;mat-expansion-panel (id)=&quot;data.id&quot; (opened)=&quot;data.panelOpenState === true&quot; (closed)=&quot;data.panelOpenState === false&quot; [expanded]=&quot;data.expanded&quot; (click)=&quot;stateValue()&quot;&gt;
        &lt;mat-expansion-panel-header&gt;
            &lt;mat-panel-title&gt;
                {{ data?.title }}
            &lt;/mat-panel-title&gt;
        &lt;/mat-expansion-panel-header&gt;
        &lt;ng-content&gt;&lt;/ng-content&gt;
    &lt;/mat-expansion-panel&gt;
&lt;/mat-accordion&gt;

Component where error is coming

this.accordianStateService.openState$.subscribe(data =&gt; {
  if (data.id === &#39;firstAccordionId&#39;) {
    this.firstDriverExpanded = true;
  } else if (data.id === &#39;secondAccordionId&#39;) {
    this.secondDriverExpanded = false;
  } else if (data.id === &#39;secondAccordionId&#39;) {
    this.thirdDriverExpanded = false;
  }
});

If I console.log(data) then I get {}. How can I set the value in the object before subscribing?

答案1

得分: 1

export interface SidePanelAccordianData {
id: string;
title?: string;
panelOpenState: boolean;
expanded?: boolean;
}

private _openStateBehaviorSubject = new BehaviorSubject<SidePanelAccordianData | undefined>(undefined);

or

private _openStateSubject = new Subject();

英文:
export interface SidePanelAccordianData {
  id: string;
  title?: string;
  panelOpenState: boolean;
  expanded?: boolean;
}

 private _openStateBehaviorSubject = new BehaviorSubject&lt;SidePanelAccordianData  | undefined&gt;(undefined);

or

  private _openStateSubject = new Subject&lt;SidePanelAccordianData&gt;();

答案2

得分: -1

你必须将_openStateBehaviorSubject配置如下:

private _openStateBehaviorSubject = new BehaviorSubject<{ id: string; state: boolean }>({} as { id: string; state: boolean });

这将指定BehaviorSubject的泛型类型。如果不传递它,泛型类型将默认为{},这就是导致错误的原因。

英文:

You have to configure your _openStateBehaviorSubject as following:

private _openStateBehaviorSubject = new BehaviorSubject&lt;{ id: string; state: boolean }&gt;({} as { id: string; state: boolean });

This will specify the generic type of the BehaviorSubject. If you don't pass it, the generic type will be just {} as the default value, and this is the cause of your error.

huangapple
  • 本文由 发表于 2023年6月19日 17:36:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76505376.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定