Angular / TS - 从字符串中获取类型<SomeClass>
Angular / TS - Get Type<SomeClass> from string
我有一个多次使用的组件来显示不同的数据。要知道要显示哪些数据,组件会得到一个字符串作为 @Input()
(我宁愿直接得到一个 Type<SomeClass>
,但这似乎不可能)。然后我使用该字符串来了解要实例化哪个 class 以获得正确的数据。但是我找不到任何方法从该字符串中获取 Type<SomeClass>
。
Html 显示卡片组件的文件:
<app-card cardData="SomeClassName"></app-card>
<!-- Here I wish I could just give the class type -->
<app-card cardData="SomeOtherClassName"></app-card>
...
服务管理所需 class 的创建:
@Injectable({
providedIn: 'root'
})
export class SomeService {
constructor(private someOtherService: SomeOtherService) {
}
getDataFromType(dataClass: Type<SomeAbstractClass>, input: any): SomeAbstractClass {
return new dataClass(this.someOtherService, input);
}
}
卡片组件的文件:
@Component({
selector: 'app-card',
templateUrl: './card.component.html',
styleUrls: ['./card.component.scss']
})
export class CardComponent implements OnInit, ISimulable {
@Input() cardData: string | undefined = undefined;
rowsData: SomeAbstractClass;
constructor(private someService: SomeService) {
rowsData = someService.getDataFromType(/* cardData as Type<SomeAbstractClass> */);
}
<div *ngFor="let data of rowsData">
<div>
{{ data.label }}
</div>
<div>
{{ data.value }}
</div>
<div>
<em>{{ data.metric }}</em>
</div>
</div>
终于找到了直接在html中传递Type<SomeAbstractClass>
的方法。我正在使用包含我希望能够传递的值的管道。
常量文件:
export const cardDataClasses = {
name : SomeAbstractClass,
...
};
export const radioOptionsDist = [
...
];
export const radioOptionsRw = [
...
];
管道:
@Pipe({
name: 'const'
})
export class ConstPipe implements PipeTransform {
transform(value: string, args?: any) {
return {
cardDataClasses: cardDataClasses,
radioOptionsDist: radioOptionsDist,
radioOptionsRw: radioOptionsRw,
};
}
}
管道的使用:
<app-card
[cardDataClass]="('' | const).cardDataClasses.desiredClass"
></app-card>
这样我就在我的方法中直接使用了所需的类型:
ngOnInit(): void {
this.rawData = this.dispatcher.getCardDataFromType(this.cardDataClass);
}
我有一个多次使用的组件来显示不同的数据。要知道要显示哪些数据,组件会得到一个字符串作为 @Input()
(我宁愿直接得到一个 Type<SomeClass>
,但这似乎不可能)。然后我使用该字符串来了解要实例化哪个 class 以获得正确的数据。但是我找不到任何方法从该字符串中获取 Type<SomeClass>
。
Html 显示卡片组件的文件:
<app-card cardData="SomeClassName"></app-card>
<!-- Here I wish I could just give the class type -->
<app-card cardData="SomeOtherClassName"></app-card>
...
服务管理所需 class 的创建:
@Injectable({
providedIn: 'root'
})
export class SomeService {
constructor(private someOtherService: SomeOtherService) {
}
getDataFromType(dataClass: Type<SomeAbstractClass>, input: any): SomeAbstractClass {
return new dataClass(this.someOtherService, input);
}
}
卡片组件的文件:
@Component({
selector: 'app-card',
templateUrl: './card.component.html',
styleUrls: ['./card.component.scss']
})
export class CardComponent implements OnInit, ISimulable {
@Input() cardData: string | undefined = undefined;
rowsData: SomeAbstractClass;
constructor(private someService: SomeService) {
rowsData = someService.getDataFromType(/* cardData as Type<SomeAbstractClass> */);
}
<div *ngFor="let data of rowsData">
<div>
{{ data.label }}
</div>
<div>
{{ data.value }}
</div>
<div>
<em>{{ data.metric }}</em>
</div>
</div>
终于找到了直接在html中传递Type<SomeAbstractClass>
的方法。我正在使用包含我希望能够传递的值的管道。
常量文件:
export const cardDataClasses = {
name : SomeAbstractClass,
...
};
export const radioOptionsDist = [
...
];
export const radioOptionsRw = [
...
];
管道:
@Pipe({
name: 'const'
})
export class ConstPipe implements PipeTransform {
transform(value: string, args?: any) {
return {
cardDataClasses: cardDataClasses,
radioOptionsDist: radioOptionsDist,
radioOptionsRw: radioOptionsRw,
};
}
}
管道的使用:
<app-card
[cardDataClass]="('' | const).cardDataClasses.desiredClass"
></app-card>
这样我就在我的方法中直接使用了所需的类型:
ngOnInit(): void {
this.rawData = this.dispatcher.getCardDataFromType(this.cardDataClass);
}