【问题标题】:Error when exporting function that returns class: Exported variable has or is using private name导出返回类的函数时出错:导出的变量具有或正在使用私有名称
【发布时间】:2017-02-23 09:40:01
【问题描述】:

我收到了错误

错误 TS4025:导出的变量“UserApiClientModule”具有或正在使用私有名称“UserApiClient”。

在以下代码中:

export var UserApiClientModule = {
  fromConfiguration: (configuration: Configuration) => {
    @NgModule({
      providers: [
        {
          provide: BASE_PATH,
          useValue: basePath
        },
        {
          provide: Configuration,
          useValue: configuration
        },
        RegistrationApi,
        AuthApi,
        AccountApi,
        ContactsApi,
        ContactOrgsApi
      ],
      imports: [
        CommonModule,
        HttpModule
      ]
    })
    class UserApiClient { }

    return UserApiClient;
  }
}

我怀疑解决方案是以某种方式导出 UserApiClient 类型,但我不确定如何执行此操作,因为它是在函数中声明的。

【问题讨论】:

  • 对于 AOT 来说可能太复杂了。还有其他方法可以将配置导入 Angular2。你可以提供它。

标签: angular typescript


【解决方案1】:

这里的重点是:

Typescript 尝试猜测所有公共部分的返回类型。不仅要猜测,还要真正将 UserApiClient 声明为 fromConfiguration 调用的返回类型的一部分

因为我们返回的东西不是导出的,是内部的.. 有一个问题。但是我们可以很容易地返回其他东西,例如一些通用界面...或者至少是神奇的any

// change this
export var UserApiClientModule = {
  fromConfiguration: (configuration: Configuration) => {
    @NgModule({
    ...


// to that
export var UserApiClientModule = {                // below is the change
  fromConfiguration: (configuration: Configuration) : any => {
    @NgModule({
    ...

我更愿意声明一些通用接口IHaveDynamicData ...实际上在此处显示的类似情况How can I use/create dynamic template to compile dynamic Component with Angular 2.0?

【讨论】:

    猜你喜欢
    • 2019-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-13
    • 2018-09-25
    • 2019-09-15
    • 2017-09-06
    相关资源
    最近更新 更多