以下代码在Dev中工作正常,但在Prod模式下不起作用:
模块
export class SpinnerModule {
static forRoot(config?: SpinnerConfig): ModuleWithProviders {
return {
ngModule: SpinnerModule,
providers: [SpinnerService, { provide: SpinnerConfigToken, useValue: { ...DEFAULT_CONFIG, ...config } }]
};
}
}
配置令牌
export const SpinnerConfigToken = new InjectionToken<SpinnerConfig>('SPINNER CONFIG TOKEN');
模型
// Project imports
import { Color, SpinnerAnimation } from '../../../models';
export interface SpinnerConfig {
label?: string;
color?: Color;
animation?: SpinnerAnimation;
}
export const DEFAULT_CONFIG: SpinnerConfig = {
label: '',
color: Color.Blue,
animation: SpinnerAnimation.DoubleBounce
};
Component
export class SpinnerComponent implements OnInit {
@Input() isLoading: boolean;
@Input() label: string;
@Input() color: Color;
@Input() animation: SpinnerAnimation;
isFullScreen: boolean;
spinnerAnimation: typeof SpinnerAnimation;
constructor(@Inject(SpinnerConfigToken) private config: SpinnerConfig) {
this.spinnerAnimation = SpinnerAnimation;
}
ngOnInit(): void {
this.isLoading = !!this.isLoading;
this.color = this.color || this.config.color;
this.label = this.label || this.config.label;
this.animation = this.animation || this.config.animation;
App模块
@NgModule({
imports: [
...
SpinnerModule.forRoot({
label: 'Loading, please wait ...'
})
],
...
})
export class AppModule {}
我想将给定的配置与我设置为默认值的内容合并。我所做的工作在Dev中效果很好,但是在Prod中,我没有错误,但是值是不确定的,看起来像合并两个对象的散布运算符不起作用,
非常感谢。
您应该创建函数以使它与aot版本一起使用。
export function DefaultConfig(config: HttpClient) {
return ({ ...DEFAULT_CONFIG, ...config })
}
和:
providers: [SpinnerService, { provide: SpinnerConfigToken, useFactory: DefaultConfig }]