本文介绍了错误错误:未捕获(承诺):错误:StaticInjectorError(AppModule)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

import { Injectable } from '@angular/core';@Injectable()导出类 ClientCacheService {私有_cacheMode:缓存模式;私有_cacheMode:缓存模式;构造函数(缓存?:CacheMode){如果(!缓存)this._cacheMode = CacheMode.SessionStorage;别的this._cacheMode = 缓存;}setToCache(key:string, prefix:string, definition: any) {if(this._cacheMode === CacheMode.SessionStorage)window.sessionStorage.setItem(`${prefix}_${key}`, JSON.stringify(definition));否则如果(CacheMode.Memory)window["`${prefix}_${key}`"] = JSON.stringify(definition);}getFromCache(键:字符串,前缀:字符串){let res = window.sessionStorage.getItem(`${prefix}_${key}`);if (res) 返回 JSON.parse(res);返回未定义;}}导出枚举缓存模式{记忆,本地存储,会话存储}

ClientCacheService类的构造函数被调用时,显示如下信息:

ERROR Error: Uncaught (in promise): Error: StaticInjectorError(AppModule)[ClientCacheService ->数字]:静态注入器错误(平台:核心)[ClientCacheService ->数字]:NullInjectorError: No provider for Number!错误:StaticInjectorError(AppModule)[ClientCacheService ->数字]:静态注入器错误(平台:核心)[ClientCacheService ->数字]:NullInjectorError: No provider for Number!

我在 this._cacheMode = cacheMode; 行上放置了一个 breakpoint 并且调试器在到达这个 breakpoint 之前启动错误.

我不明白为什么会显示此错误.任何人都知道这意味着什么以及如何解决它?

解决方案

您正在尝试注入不可注入的枚举,您应该实现一个具有单一接口的抽象类,用于 Memory、LocalStorage 和 SessionStorage,我们将其称为 StorageService例如,接下来您应该像这样在构造函数中使用它:

constructor(storageService: StorageService) {}

最后是提供者数组

提供者:[{ provide: StorageService, useClass: Memory}//或 LocalStorage 或 SessionStorage]

I have the following code:

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
}

When the constructor of the class ClientCacheService is called, it is shown the following message:

I have put a breakpoint on the line this._cacheMode = cacheMode; and the debugger launches the error before reach this breakpoint.

I'm not understanding why this errors is been shown. Any one have any idea what this means and how to solve it?

解决方案

You are trying to Inject enum that is not injectable, you should implement an abstract class with a single interface for Memory, LocalStorage and SessionStorage, Lets call it StorageService for example, next you should consume it in the constructor like this:

constructor(storageService: StorageService) {}

finally the providers array

providers: [
    { provide: StorageService, useClass: Memory} // or LocalStorage or SessionStorage
]

这篇关于错误错误:未捕获(承诺):错误:StaticInjectorError(AppModule)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 01:26