我有以下代码:
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
的构造函数时,将显示以下消息:
ERROR Error: Uncaught (in promise): Error: StaticInjectorError(AppModule)[ClientCacheService -> Number]: StaticInjectorError(Platform: core)[ClientCacheService -> Number]: NullInjectorError: No provider for Number! Error: StaticInjectorError(AppModule)[ClientCacheService -> Number]: StaticInjectorError(Platform: core)[ClientCacheService -> Number]: NullInjectorError: No provider for Number!
我在行breakpoint
上放置了this._cacheMode = cacheMode;
,调试器在到达此breakpoint
之前启动了错误。
我不明白为什么会显示此错误。任何人都知道这意味着什么以及如何解决吗?
答案 0 :(得分:1)
您试图注入不可注入的枚举,您应该为Memory,LocalStorage和SessionStorage实现一个具有单个接口的抽象类,例如将其命名为StorageService,接下来,您应在构造函数中使用它,如下所示:< / p>
implements
最后是providers数组
Component