4

I am setting up a library that I would like my other projects to use. I am trying to pass a config file to my module which I have defined like this:

@NgModule({
  declarations: [ImageComponent],
  imports: [CommonModule],
})
export class ImagesModule {
  static forRoot(config: ImagesConfig): ModuleWithProviders<ImagesModule> {
    return {
      ngModule: ImagesModule,
      providers: [{ provide: ImagesConfig, useValue: config }],
    };
  }
}

export class ImagesConfig {
  cloudName: string;
}

This looks ok, but now I would like to use the cloudName in the actual module, like this:

@NgModule({
  declarations: [ImageComponent],
  imports: [
    CommonModule,

    CloudinaryModule.forRoot(Cloudinary, {
      config.cloudName,
    }),
  ],
})
export class ImagesModule {
  static forRoot(config: ImagesConfig): ModuleWithProviders<ImagesModule> {
    return {
      ngModule: ImagesModule,
      providers: [{ provide: ImagesConfig, useValue: config }],
    };
  }
}

export class ImagesConfig {
  cloudName: string;
}

Is this possible? If so, how can I do it?


I have created the module as such:

import { ModuleWithProviders, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ImageComponent } from './image.component';

import { CloudinaryModule } from '@cloudinary/angular-5.x';
import * as Cloudinary from 'cloudinary-core';

import { ImagesConfig } from './images-config';
import { IMAGES_CONFIG } from './images-config.token';

@NgModule({
  declarations: [ImageComponent],
  imports: [CommonModule],
})
export class ImagesModule {
  static forRoot(config: ImagesConfig): ModuleWithProviders<ImagesModule> {
    return {
      ngModule: ImagesModule,
      providers: [
        { provide: IMAGES_CONFIG, useValue: config },
        ...CloudinaryModule.forRoot(Cloudinary, {
          cloud_name: config.cloudName,
        }).providers,
      ],
    };
  }
}

images-config.ts and images-config.token.ts look like this, respectively:

export interface ImagesConfig {
  cloudName: string;
}

and

import { InjectionToken } from '@angular/core';

export const IMAGES_CONFIG = new InjectionToken('IMAGES_CONFIG');

When I try to use in the public-api.ts:

/*
* Public API Surface of situ-angular-components
*/

export * from './lib/images/images-config';


export * from './lib/images/images.module';

It all compiles, but if I try to build my library I get this error:

enter image description here


Unfortunately, the answer @yurzui provided doesn't appear to work. If I change my module to this:

@NgModule({
  declarations: [ImageComponent],
  imports: [
    CommonModule,
    CloudinaryModule.forRoot(Cloudinary, { cloud_name: 'demo' }),
  ],
})
export class ImagesModule {}

And build my library, it works. If I do it like this:

@NgModule({
  declarations: [ImageComponent],
  imports: [CommonModule],
})
export class ImagesModule {
  static forRoot(config: ImagesConfig): ModuleWithProviders<ImagesModule> {
    return {
      ngModule: ImagesModule,
      providers: [
        { provide: IMAGES_CONFIG, useValue: config },
        ...CloudinaryModule.forRoot(Cloudinary, {
          cloud_name: config.cloudName,
        }).providers,
      ],
    };
  }
}

When I try to build the library, I get an error stating:

If 'cl-image' is an Angular component, then verify that it is part of this module.

I am using cl-image in the ImageComponent.

1 Answer 1

7

You can try the following:

@NgModule({
  declarations: [ImageComponent],
  imports: [
    CommonModule,
    CloudinaryModule,
  ],
})
export class ImagesModule {
  static forRoot(config: ImagesConfig): ModuleWithProviders<ImagesModule> {
    return {
      ngModule: ImagesModule,
      providers: [
        { provide: ImagesConfig, useValue: config },
        ...CloudinaryModule.forRoot(Cloudinary, {
          // pass config.cloudName here,
        }).providers,
      ],
    };
  }
}

export class ImagesConfig {
  cloudName: string;
}
Sign up to request clarification or add additional context in comments.

9 Comments

that compiles, but when I add it to public-api.ts and try to build, I get an error: Cannot read property 'toString' of undefined
Can you show me screenshot of that error? How did you pass cloudName?
Looks like this error comes from postcss plugin. It shouldn't be related to how you wrote ts code. At least this is my expectation
weird, I don't use postcss
Figured it out :) lol it was in the component, it was looking for a stylesheet that didn't exist
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.