Angular 使用 forRoot 的库
Angular library using forRoot
我正在建立一个我希望我的其他项目使用的库。
我正在尝试将配置文件传递到我定义如下的模块:
@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,像这样:
@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;
}
这可能吗?如果可以,我该怎么做?
我创建了这样的模块:
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';
全部编译,但如果我尝试构建我的库,我会收到此错误:
不幸的是,@yurzui 提供的答案似乎不起作用。
如果我将我的模块更改为:
@NgModule({
declarations: [ImageComponent],
imports: [
CommonModule,
CloudinaryModule.forRoot(Cloudinary, { cloud_name: 'demo' }),
],
})
export class ImagesModule {}
并建立我的图书馆,它有效。
如果我这样做:
@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,
],
};
}
}
当我尝试构建库时,我收到一条错误消息:
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;
}
我正在建立一个我希望我的其他项目使用的库。 我正在尝试将配置文件传递到我定义如下的模块:
@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,像这样:
@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;
}
这可能吗?如果可以,我该怎么做?
我创建了这样的模块:
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';
全部编译,但如果我尝试构建我的库,我会收到此错误:
不幸的是,@yurzui 提供的答案似乎不起作用。 如果我将我的模块更改为:
@NgModule({
declarations: [ImageComponent],
imports: [
CommonModule,
CloudinaryModule.forRoot(Cloudinary, { cloud_name: 'demo' }),
],
})
export class ImagesModule {}
并建立我的图书馆,它有效。 如果我这样做:
@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,
],
};
}
}
当我尝试构建库时,我收到一条错误消息:
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;
}