Typescript 错误:未捕获(承诺中):错误:StaticInjectorError(AppModule)
我有以下代码:Typescript 错误:未捕获(承诺中):错误:StaticInjectorError(AppModule),typescript,angular5,Typescript,Angular5,我有以下代码: import { Injectable } from '@angular/core'; @Injectable() export class ClientCacheService { private _cacheMode: CacheMode; private _cacheMode: CacheMode; constructor(cache?: CacheMode) { if(!cache) this._cache
import { Injectable } from '@angular/core';
@Injectable()
export class ClientCacheService {
private _cacheMode: CacheMode;
private _cacheMode: CacheMode;
constructor(cache?: CacheMode) {
if(!cache)
this._cacheMode = CacheMode.SessionStorage;
else
this._cacheMode = cache;
}
setToCache(key :string, prefix:string, definition: any) {
if(this._cacheMode === CacheMode.SessionStorage)
window.sessionStorage.setItem(`${prefix}_${key}`, JSON.stringify(definition));
else if(CacheMode.Memory)
window["`${prefix}_${key}`"] = JSON.stringify(definition);
}
getFromCache(key :string, prefix:string) {
let res = window.sessionStorage.getItem(`${prefix}_${key}`);
if (res) return JSON.parse(res);
return undefined;
}
}
export enum CacheMode{
Memory,
LocalStorage,
SessionStorage
}
调用类ClientCacheService
的构造函数时,将显示以下消息:
我在这行上放置了一个断点代码>,调试器在到达此断点之前启动错误
我不明白为什么会出现这种错误。任何人都知道这意味着什么以及如何解决它吗?如果您试图注入不可注入的枚举,您应该为内存、本地存储和会话存储实现一个带有单个接口的抽象类,让我们将其称为StorageService,例如,接下来您应该在构造函数中使用它,如下所示:
constructor(storageService: StorageService) {}
最后是提供者数组
providers: [
{ provide: StorageService, useClass: Memory} // or LocalStorage or SessionStorage
]
什么值应该作为缓存传递?@abetteroliver枚举
providers: [
{ provide: StorageService, useClass: Memory} // or LocalStorage or SessionStorage
]