在 Angular 应用程序的编译中找不到声明的 InjectionToken
Declared InjectionToken cannot be found on compile of Angular application
我正在尝试通过使用 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.module.ts:112:21-32 - Error: export 'NUMBER_MASK' (imported as 'NUMBER_MASK') was not found in './services/system/masks.service' (possible exports: NumberMask, NumberMaskFactory)
(注1)例如MSAL_INSTANCE
在这个sample.
要创建注入令牌,您必须实例化
export const NUMBER_MASK = new InjectionToken<string>('');
我正在尝试通过使用 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.module.ts:112:21-32 - Error: export 'NUMBER_MASK' (imported as 'NUMBER_MASK') was not found in './services/system/masks.service' (possible exports: NumberMask, NumberMaskFactory)
(注1)例如MSAL_INSTANCE
在这个sample.
要创建注入令牌,您必须实例化
export const NUMBER_MASK = new InjectionToken<string>('');