英文:
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 'id' does not exist on type '{}'.
Here is my code
Service
import { Injectable } from '@angular/core';
import { BehaviorSubject, Observable, Subject } from 'rxjs';
@Injectable({
providedIn: 'root'
})
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 '@angular/core';
import { SidePanelAccordianData } from './side-panel-accordian.model';
import { SidePanelAccordianService } from './side-panel-accordian.service';
@Component({
selector: 'app-side-panel-accordian',
templateUrl: './side-panel-accordian.component.html',
styleUrls: ['./side-panel-accordian.component.scss']
})
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 = '';
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>
Component where error is coming
this.accordianStateService.openState$.subscribe(data => {
if (data.id === 'firstAccordionId') {
this.firstDriverExpanded = true;
} else if (data.id === 'secondAccordionId') {
this.secondDriverExpanded = false;
} else if (data.id === 'secondAccordionId') {
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<SidePanelAccordianData | undefined>(undefined);
or
private _openStateSubject = new Subject<SidePanelAccordianData>();
答案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<{ id: string; state: boolean }>({} 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论