Reputation: 5104
I have a custom library and a demo project both on angular 7. The library is building fine, the demo project as well and they're also running correctly. The problem happens when I try to build the demo site using the prod tag ng build --prod
. Then I get the warning and error below:
Warning: Can't resolve all parameters for DialogService in /Users/eestein/Documents/dev/sdk-front/node_modules/@proj/components/proj-components.d.ts: ([object Object], [object Object], ?, ?, [object Object], [object Object]). This will become an error in Angular v6.x
Warning: Can't resolve all parameters for ɵa in /Users/eestein/Documents/dev/sdk-front/node_modules/@proj/components/proj-components.d.ts: (?). This will become an error in Angular v6.x
The error:
ERROR in : Can't resolve all parameters for ɵbw in /Users/eestein/Documents/dev/sdk-front/node_modules/@proj/components/proj-components.d.ts: ([object Object], [object Object], [object Object], [object Object], ?, [object Object], [object Object]).
I tracked the warnings and errors down to the offending parts, they are:
...
constructor(
private overlay: OverlayService,
private injector: Injector,
@Optional() @Inject(SdkConstants.Dialog.DefaultOptionsInjectionToken) private defaultOptions: DialogConfig,
@Inject(SdkConstants.Dialog.ScrollStrategyInjectionToken) scrollStrategy: any,
@Optional() @SkipSelf() private parentDialog: DialogService,
private overlayContainer: OverlayContainerService
) {
this.scrollStrategy = scrollStrategy;
console.group('dialog');
console.log('defaultOptions', defaultOptions);
console.log('scrollStrategy', scrollStrategy);
console.groupEnd();
}
...
The compiler is complaining it can't resolve defaultOptions and scrollStrategy, even though the first is marked as optional and the second is provided:
export function dialogScrollStrategyProviderFactory(overlay: OverlayService): () => BaseScrollStrategy {
const result = () => overlay.scrollStrategies.block();
return result;
}
...
providers: [
DialogService,
{
provide: SdkConstants.Dialog.ScrollStrategyInjectionToken,
deps: [OverlayService],
useFactory: dialogScrollStrategyProviderFactory
}
]
constructor(
private dialogService: DialogService,
private overlayService: OverlayService,
private ngZone: NgZone,
private viewContainerRef: ViewContainerRef,
@Inject(SdkConstants.Datepicker.ScrollStrategyInjectionToken) scrollStrategy: any,
@Optional() private dateService: DateService,
@Optional() @Inject(DOCUMENT) document: any
) {
this.document = document;
this.scrollStrategy = scrollStrategy;
console.group('datepicker');
console.log('scrollStrategy', this.scrollStrategy);
console.groupEnd();
}
The compiler is complaining it can't resolve scrollStrategy, even though it is provided.
Output from both console.logs:
As you can see, if I run the demo project and the referred components everything works just fine. It's only when I run ng build --prod
on the demo project I get the build error.
For the past two weeks I have been reading a lot of issues on GitHub and SO questions about "Can't resolve all parameters for ..." and others, but not one seems to be directly related to my problem.
Any ideas on what could be wrong?
PS: Please let me know if more bits of code would help to identify the problem better.
Update (adding InjectionToken's definition):
export namespace SdkConstants {
export class Dialog {
public static DefaultOptionsKey = 'dialog-default-options';
public static DefaultOptionsInjectionToken =
new InjectionToken<() => BaseScrollStrategy>(Dialog.DefaultOptionsKey);
}
}
Upvotes: 2
Views: 1269
Reputation: 12052
I managed to reproduce your issue and the problem is that you use a namespace for the constants. Using namespaces is not recommended in Angular. Check also this post.
Here is a sample how you can emulate the same structure using a class instead of a namespace this solves the build issue.
export class Dialog {
public static DefaultOptionsKey = 'dialog-default-options';
public static DefaultOptionsInjectionToken =
new InjectionToken<ScrollStrategyProducer>(Dialog.DefaultOptionsKey);
}
export class SdkConstants {
public static Dialog: typeof Dialog = Dialog;
}
Upvotes: 1