Angular 如何使用NgModule导出const

Angular 如何使用NgModule导出const,angular,typescript,datepicker,angular5,ng-modules,Angular,Typescript,Datepicker,Angular5,Ng Modules,我有一个常数,应该可以被其他模块使用 我有一个Datepicker指令,它定义了日期格式常量(我有这样的指令,所以使用我的指令的人可以使用这些常量): 以及用于进一步导出它们的模块。构建此模块,然后在其他项目/模块中使用 import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { DatepickerDirective, TIME_FORMAT, DATETI

我有一个常数,应该可以被其他模块使用

我有一个Datepicker指令,它定义了日期格式常量(我有这样的指令,所以使用我的指令的人可以使用这些常量):

以及用于进一步导出它们的模块。构建此模块,然后在其他项目/模块中使用

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { DatepickerDirective, TIME_FORMAT, DATETIME_FORMAT, DATE_FORMAT } from './datepicker.directive';

@NgModule({
  imports: [
    CommonModule
  ],
  exports: [
    DatepickerDirective,
    DATE_FORMAT,
    DATETIME_FORMAT,
    TIME_FORMAT
  ],
  declarations: [
    DatepickerDirective
  ]
})
export class DatepickerModule { }
但在尝试构建它时,我得到了一个错误:

ERROR in src/app/datepicker/datepicker.module.ts(5,11): error TS2345: Argument of type '{ imports: (typeof CommonModule)[]; exports: (string | typeof DatepickerDirective)[]; declara...' is not assignable to parameter of type 'NgModule'.
  Types of property 'exports' are incompatible.
    Type '(string | typeof DatepickerDirective)[]' is not assignable to type '(any[] | Type<any>)[]'.
      Type 'string | typeof DatepickerDirective' is not assignable to type 'any[] | Type<any>'.
        Type 'string' is not assignable to type 'any[] | Type<any>'.
src/app/datepicker/datepicker.module.ts(5,11)中的
错误:错误TS2345:类型为“{imports:(typeof CommonModule)[];exports:(string | typeof DatepickerDirective)[];declara…”的参数不能分配给类型为“NgModule”的参数。
属性“导出”的类型不兼容。
类型“”(字符串| typeof DatepickerDirective)[]”不能分配给类型“”(任何[]|类型)[]”。
类型“string | typeof DatepickerDirective”不可分配给类型“any[]| Type”。
类型“string”不可分配给类型“any[]| Type”。

NgModule无法导出常量,但可以使用InjectionToken提供值。 查找有关InjectionToken的更多信息


将其放入环境文件中。导出datepickerdirective中的常量
ERROR in src/app/datepicker/datepicker.module.ts(5,11): error TS2345: Argument of type '{ imports: (typeof CommonModule)[]; exports: (string | typeof DatepickerDirective)[]; declara...' is not assignable to parameter of type 'NgModule'.
  Types of property 'exports' are incompatible.
    Type '(string | typeof DatepickerDirective)[]' is not assignable to type '(any[] | Type<any>)[]'.
      Type 'string | typeof DatepickerDirective' is not assignable to type 'any[] | Type<any>'.
        Type 'string' is not assignable to type 'any[] | Type<any>'.
import { ModuleWithProviders, NgModule, InjectionToken } from '@angular/core';
import { CommonModule } from '@angular/common';
import { DatepickerDirective, TIME_FORMAT, DATETIME_FORMAT, DATE_FORMAT } from './datepicker.directive';

export interface IDatepickerFormats {
  TIME_FORMAT: string;
  DATETIME_FORMAT: string;
  DATE_FORMAT: string;
}

export const DATEPICKER_FORMATS = new InjectionToken<IDatepickerFormats>('datepicker.formats');

@NgModule({
  imports: [CommonModule],
  exports: [DatepickerDirective],
  declarations: [DatepickerDirective]
})
export class DatepickerModule {
  static forRoot(): ModuleWithProviders {
    return {
      ngModule: DatepickerModule,
      providers: [
        {
          provide: DATEPICKER_FORMATS,
          useValue: {TIME_FORMAT, DATETIME_FORMAT, DATE_FORMAT}
        }
      ]
    };
  }
}
import { Component, Inject} from '@angular/core';
import { DATEPICKER_FORMATS, IDatepickerFormats } from './datepicker';

@Component({
  selector: 'app-example',
  templateUrl: './app-example.component.html',
  styleUrls: ['./app-example.component.scss']
})
export class AppExampleComponent {
  private DATE_FORMAT: string;
  private TIME_FORMAT: string;
  private DATETIME_FORMAT: string;

  constructor(@Inject(DATEPICKER_FORMATS) private datePickerFormats: IDatepickerFormats) {
    this.DATE_FORMAT = this.datePickerFormats.DATE_FORMAT;
    this.TIME_FORMAT = this.datePickerFormats.TIME_FORMAT;
    this.DATETIME_FORMAT = this.datePickerFormats.DATETIME_FORMAT;
  }
}