我试图通过使用
InjectionToken
。但是,在编译应用程序的源代码中找不到声明的常量。我以前见过这种模式有效(1),但我不确定我做的有什么不同。下面是我的代码摘要。
// ./services/system/masks.service
export declare const NUMBER_MASK: InjectionToken<string>;
export function NumberMaskFactory(): NumberMask {
const lang = navigator.language.substring(0, 2);
if (lang == "en") {
const radix: string = getLocaleNumberSymbol(navigator.language, NumberSymbol.Decimal);
const thousandSeperator: string = getLocaleNumberSymbol(navigator.language, NumberSymbol.Group);
return new NumberMask(radix, thousandSeperator);
}
return new NumberMask(".", ",");
}
export class NumberMask {
public radix: string;
public thousandSeparator: string;
constructor(radix: string, thousandSeparator: string) {
this.radix = radix;
this.thousandSeparator = thousandSeparator;
}
}
// app.module.ts
import { NumberMaskFactory, NUMBER_MASK } from "./services/system/masks.service";
// ...
providers: [
{
provide: NUMBER_MASK,
useFactory: NumberMaskFactory
}
// ...
编译时,这会引发以下错误:
./src/app/app。单元ts:112:21-32-错误:在中找不到导出“数字掩码”(导入为“数字掩码”)/服务/系统/口罩。服务(可能的出口:NumberMask、NumberMaskFactory)
(注1)例如:
MSAL_INSTANCE
在这个
sample
.