以下代码在 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
};
零件
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;
应用模块
@NgModule({
imports: [
...
SpinnerModule.forRoot({
label: 'Loading, please wait ...'
})
],
...
})
export class AppModule {}
我想将给定的配置与我设置为默认值的配置合并。我所做的在 Dev 中效果很好,但在 Prod 中,我没有错误,但值是未定义的,看起来用于合并两个对象的扩展运算符不起作用,
非常感谢。