简体   繁体   English

元素隐式具有“任何”类型,因为“任何”类型的表达式不能用于索引类型,任何人都可以告诉我

[英]Element implicitly has an 'any' type because expression of type 'any' can't be used to index type any one can tell me

hello im doing dynamic component to gridster2 and i have a problem in "components[this.componentRef];"你好我正在对gridster2做动态组件,我在“组件[this.componentRef];”中有问题is show me this error:Element implicitly has an 'any' type because expression of type 'any' can't be used to index type '{ example1: typeof Example1Component;向我显示此错误:元素隐含地具有“任何”类型,因为“任何”类型的表达式不能用于索引类型“{ example1:typeof Example1Component; example2: typeof Example2Component;示例 2:示例 2 组件的类型; }' }'

any help please请有任何帮助

import { Directive, Input, OnChanges, ViewContainerRef, ComponentFactoryResolver, ComponentRef } from '@angular/core';

import {Example1Component} from '../../../Components/example1/example1.component';
import {Example2Component} from '../../../Components/example2/example2.component';
const components = {
  example1: Example1Component,
  example2: Example2Component
};
@Directive({
  selector: '[appLayoutItem]'
})
export class LayoutItemDirective implements OnChanges {
  @Input() componentRef!: any;
  component!: ComponentRef<any>;

  constructor(
    private container: ViewContainerRef,
    private resolver: ComponentFactoryResolver
  ) { }
  ngOnChanges(): void {
    const component = components[this.componentRef];

    if (component) {
      const factory = this.resolver.resolveComponentFactory<any>(component);
      this.component = this.container.createComponent(factory);
    }
  }
}

I'm not really sure what exactly you are tring to achieve, but to me it looks like it should be like this (I did not try this code due to the lack of further information).我不太确定你到底想达到什么目标,但对我来说它应该是这样的(由于缺乏进一步的信息,我没有尝试这段代码)。

const components = {
  example1: Example1Component,
  example2: Example2Component
}; 

@Input() componentRef!: 'example1' | 'example2'; // 
 component!: ComponentRef<Example1Component | Example2Component>;

Then this access should actually work:那么这个访问应该真的有效:

 const component = components[this.componentRef];

You could consider using an Enum for 'example1' |您可以考虑为“example1”使用枚举 | 'example2' as well, that's what I personally like to do but it's personal preference. 'example2'也是如此,这是我个人喜欢做的,但这是个人喜好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 元素隐式具有“任何”类型,因为类型“徽标”的表达式不能用于索引 Angular 中的类型“对象” - Element implicitly has an 'any' type because expression of type '"logo"' can't be used to index type 'Object' in Angular 元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引 Angular 中的类型 - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type in Angular 元素隐式具有“任何”类型,因为“名称”类型的表达式不能用于索引类型“对象” - Element implicitly has an 'any' type because expression of type '"name"' can't be used to index type 'Object' 元素隐式具有“任何”类型,因为“事件组”类型的表达式不能用于索引类型“MonthViewDay”<eventgroupmeta> '</eventgroupmeta> - Element implicitly has an 'any' type because expression of type '"eventGroups"' can't be used to index type 'MonthViewDay<EventGroupMeta>' 元素隐式具有“任何”类型,因为“_popupComponentRef”类型的表达式不能用于索引类型“MatDatepicker”<Date> &#39; - Element implicitly has an 'any' type because expression of type '"_popupComponentRef"' can't be used to index type 'MatDatepicker<Date>' 元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型“搜索查询” - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'SearchQuery' 元素隐式具有“任何”类型,因为“数字”类型的表达式不能用于索引“对象”类型 - Element implicitly has an 'any' type because expression of type 'number' can't be used to index type 'Object' '元素隐式具有'any'类型,因为'string'类型的表达式不能用于*ngFor中的Object中的类型索引 - 'Element implicitly has an 'any' type because expression of type 'string' can't be used to index type in Object' in *ngFor 元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引“对象”类型 TS7053 - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Object' TS7053 TS7053:元素隐式具有“任何”类型,因为类型“字符串”的表达式不能用于索引类型“对象” - TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Object'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM