$get
我正在尝试升级 AngularJS 服务提供者(TranslateProvider),它从它的函数返回一个接口的实例。
问题是,在 Angular 提供程序对象 (translateProvider) 中,该provide
字段应该是Translate
但不能使用,因为 TypeScript 接口仅用于编译时。这是我得到的错误:
TS2693: 'Translate' only refers to a type, but is being used as a value here.
这是源代码的要点:
export class TranslateProvider implements ng.IServiceProvider {
public $get() {
const service: Translate = <Translate> function translate(key: string, parameters: Dictionary<StringLike>): string {
return service;
}
}
export interface Translate {
(translationKey: string, parameters?: Dictionary<StringLike>): string;
}
export function translateFactory(i: any) {
return i.get('translate');
}
export const translateProvider = {
provide: TranslateProvider,
useFactory: translateFactory,
deps: ['$injector']
};
@NgModule({
imports: [
BrowserModule,
UpgradeModule,
],
declarations: [
],
providers: [
translateProvider,
]
})