使用forRoot的Angular库 [英] Angular library using forRoot
问题描述
我正在设置一个我希望其他项目使用的库.我正在尝试将配置文件传递给我定义的模块,如下所示:
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;
}
看起来不错,但现在我想在实际模块中使用 cloudName ,如下所示:
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 和 images-config.token.ts 分别如下所示:
export interface ImagesConfig {
cloudName: string;
}
和
import { InjectionToken } from '@angular/core';
export const IMAGES_CONFIG = new InjectionToken('IMAGES_CONFIG');
当我尝试在 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:
不幸的是,@ yurzui提供的答案似乎无效.如果我将模块更改为此:
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:
如果"cl-image"是Angular组件,则请验证它是否属于此模块.
If 'cl-image' is an Angular component, then verify that it is part of this module.
我正在 ImageComponent 中使用 cl-image .
推荐答案
您可以尝试以下操作:
@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;
}
这篇关于使用forRoot的Angular库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!