angular - 导出返回类 : Exported variable has or is using private name 的函数时出错

标签 angular typescript

我遇到了错误

error TS4025: Exported variable 'UserApiClientModule' has or is using private name '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 类型,但我不确定该怎么做,因为它是在函数中声明的。

最佳答案

这里的重点是:

Typescript tries to guess what is the returned type of all the public parts. And not only guess, but to really declare the UserApiClient as a part of returned type of the fromConfiguration call

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

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


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

我宁愿声明一些通用接口(interface) IHaveDynamicData ... 实际上与这里显示的类似情况一样 How can I use/create dynamic template to compile dynamic Component with Angular 2.0?

关于angular - 导出返回类 : Exported variable has or is using private name 的函数时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40034979/

相关文章:

javascript - 云函数 onCall - 如何返回对象

typescript - 无法从 TypeScript 中的文件加载类

typescript - Typescript 和 Browserify 可以一起使用吗?

typescript - 类型 'matchAll' 上不存在属性 'string'

TypeScript 转换 json 数组

css - 如何在 bootstrap4 中添加自定义断点以及如何在 scss 中使用响应式断点混合

angular - 辅助路由是否仅适用于根组件?

angular - 在 Angular 5 环境中使用 process.env

angular - ngFor 依赖于另一个 ngFor 来完成渲染 - 获取 ExpressionChangedAfterItHasBeenCheckedError [Angular 2]

angular - 即使 Angular CLI 可以构建,Webclipse 也找不到模块