【问题标题】:How to pass configuration parameters to angular libraries?如何将配置参数传递给角度库?
【发布时间】:2020-04-16 11:34:02
【问题描述】:

我正在为一些现有的(并且不受我控制的)身份验证库(例如 keycloak-angular )构建一个包装库“auth-lib”。 我包装的身份验证库需要在应用程序初始化或引导过程中使用配置参数进行初始化图书馆。

如何通过我的包装库将配置参数从我的示例应用程序传递到身份验证库?

【问题讨论】:

    标签: angular configuration bootstrapping angular-library angular2-bootstrapping


    【解决方案1】:

    您可以使用 forRoot 将 App.Module 中的配置传递给您的库模块。

    试试这样:

    App.module.ts

     imports: [
          BrowserModule,
          AuthLibModule.forRoot({
            configuration: {configA : "abc", configB : "xyz"}
          })
       ]
    

    AuthLibModule.module.ts

    export class AuthLibModule{ 
      static forRoot(configuration): ModuleWithProviders {
        console.log(configuration);
        return {
          ngModule: AuthLibModule,
          providers: [AuthLibService,{provide: 'config', useValue: configuration}]
        };
      }
    }
    

    AuthLibService.service:

    export class AuthLibService{
    
      constructor(@Inject('config') private config:any) {
        console.log(config)
       }
    }
    

    【讨论】:

    • 当我在一个单独的项目中这样做时,它起作用了;但是将它移到我的项目中并没有,所以我的项目设置存在问题,我需要弄清楚。谢谢你的回答。
    • 嘿@Adrita - 当我这样做时参数类型(配置:{configB:string,configA:string}}不可分配给参数类型Routes。我已经尝试了很多次并且不能绕过这个错误 - 我的语法似乎有问题 - 有什么想法吗?如果有帮助,我可以发布更多代码。谢谢
    • @Voltan 你能在stackbiltz中分享你的代码吗
    • @Adrita - 感谢您的回复,Intellij 给了我一个虚假的错误。现在一切正常,所以我将关闭它。
    • @Voltan 太棒了。如果这对你有帮助,请点赞:)
    猜你喜欢
    • 2019-05-05
    • 2017-08-23
    • 1970-01-01
    • 2014-09-16
    • 2019-05-19
    • 2017-04-28
    • 1970-01-01
    • 2021-06-05
    • 1970-01-01
    相关资源
    最近更新 更多