无法在 useValue 中分配 const 值

unable to assign const value in useValue

我创建了一个 const class ElementConst

export class ElementConst {
    public static LINK = 'LINK';
    public static HEADING_1 = 'heading-1';
    public static HEADING_2 = 'heading-2';
    public static HEADING_3 = 'heading-3';
    public static HEADING_4 = 'heading-4';
    public static HEADING_5 = 'heading-5';
    public static HEADING_6 = 'heading-6';
    public static PARAGRAPH = 'paragraph';
    public static STRONG = 'BOLD';
}

所以我在我的模块中导入了这个 class 并尝试分配它 useValue

export const CONTENT_MAPPINGS_PROVIDER: Provider = [
  {
    provide: CONTENT_MAPPINGS,
    useValue: {
      ElementConst.HEADING_1: HeadingComponent, // trying to do something like this, which throws error
      'heading-2': HeadingComponent,
      'heading-3': HeadingComponent,
      'heading-4': HeadingComponent,
      'heading-5': HeadingComponent,
      'heading-6': HeadingComponent,
      'paragraph':ParagraphComponent
    }
  }
];

为什么我无法在 useValue 中赋值?

指定动态密钥时,需要将其包装在[]

下面是如何让您的代码通过编译器的简单示例:

export class ElementConst {
    public static LINK = 'LINK';
    public static HEADING_1 = 'heading-1';
    public static HEADING_2 = 'heading-2';
    public static HEADING_3 = 'heading-3';
    public static HEADING_4 = 'heading-4';
    public static HEADING_5 = 'heading-5';
    public static HEADING_6 = 'heading-6';
    public static PARAGRAPH = 'paragraph';
    public static STRONG = 'BOLD';
}

interface Provider {
  provide: any;
  useValue: any;
}

const HeadingComponent = {};
const ParagraphComponent = {};

export const CONTENT_MAPPINGS_PROVIDER: Provider[] = [
  {
    provide: '',
    useValue: {
      [ElementConst.HEADING_1]: HeadingComponent,
      'heading-2': HeadingComponent,
      'heading-3': HeadingComponent,
      'heading-4': HeadingComponent,
      'heading-5': HeadingComponent,
      'heading-6': HeadingComponent,
      'paragraph': ParagraphComponent,
    }
  }
];