构建和发布angular2模块
我已经为angular2编写了一个远程记录器,运行良好。我现在想将它作为自己的npm库发布,并努力使其可用。我已经将自己定位在模块的设置上 我在内部使用的记录器的一般设置与中描述的类似: 我尝试将这种方法应用到它自己的模块中,如下所示:构建和发布angular2模块,angular,typescript,dependency-injection,npm,angular2-modules,Angular,Typescript,Dependency Injection,Npm,Angular2 Modules,我已经为angular2编写了一个远程记录器,运行良好。我现在想将它作为自己的npm库发布,并努力使其可用。我已经将自己定位在模块的设置上 我在内部使用的记录器的一般设置与中描述的类似: 我尝试将这种方法应用到它自己的模块中,如下所示: import { NgModule, ModuleWithProviders } from '@angular/core'; import { CommonModule } from '@angular/common'; import { Log4ngSer
import { NgModule, ModuleWithProviders } from '@angular/core';
import { CommonModule } from '@angular/common';
import {
Log4ngService,
ILog4ngServiceConfig,
LOG4NG_SERVICE_CONFIG,
LOG4NG_SERVICE_HANDLER_PROVIDERS,
ErrorHandlerLoggingService,
LOG4NG_ERROR_HANDLER_CONFIG,
LOG4NG_ERROR_HANDLER_PROVIDERS
} from './src';
export * from './src';
@NgModule({
imports: [
CommonModule
],
providers: [
Log4ngService,
ErrorHandlerLoggingService
]
})
export class Log4ngModule {
static forRoot(config: ILog4ngServiceConfig = LOG4NG_SERVICE_CONFIG): ModuleWithProviders {
return {
ngModule: Log4ngModule,
providers: LOG4NG_SERVICE_HANDLER_PROVIDERS
};
}
static forChild(config: ILog4ngServiceConfig = LOG4NG_SERVICE_CONFIG): ModuleWithProviders {
return {
ngModule: Log4ngModule,
providers: LOG4NG_SERVICE_HANDLER_PROVIDERS
};
}
}
我继续创建一个演示应用程序来使用这个模块,并使用npm link
在本地使用库。当它完全构建时,当尝试在演示应用程序中使用它时,我得到运行时错误:
Error: Can't resolve all parameters for Log4ngService: (?)
我不理解,因为据我所知,所有必要的DI令牌都已正确设置
您可以在此git存储库的dev分支中找到代码:
感谢任何帮助和提示-提前感谢 由于问题出在
Log4ngService
中,您可以使用这样的工厂
/** Log4ngService factory */
export function Log4ngServiceFactory(config: ILog4ngServiceConfig ) {
return new Log4ngService (config);
}
@NgModule({
imports: [
CommonModule
],
providers: [
ErrorHandlerLoggingService
]
})
export class Log4ngModule {
static forRoot(config: ILog4ngServiceConfig = LOG4NG_SERVICE_CONFIG): ModuleWithProviders {
return {
ngModule: Log4ngModule,
providers: [
{
provide: Log4ngService,
useFactory: Log4ngServiceFactory,
deps : [config]
}
]
};
}
}
我不明白为什么要使用forRoot和forChild,因为你没有使用它们的参数
config
?是的,这些都是ngx翻译的残余和解决问题的一些尝试。应该删除那些。
/** Log4ngService factory */
export function Log4ngServiceFactory(config: ILog4ngServiceConfig ) {
return new Log4ngService (config);
}
@NgModule({
imports: [
CommonModule
],
providers: [
ErrorHandlerLoggingService
]
})
export class Log4ngModule {
static forRoot(config: ILog4ngServiceConfig = LOG4NG_SERVICE_CONFIG): ModuleWithProviders {
return {
ngModule: Log4ngModule,
providers: [
{
provide: Log4ngService,
useFactory: Log4ngServiceFactory,
deps : [config]
}
]
};
}
}