尝试将Window注入到IE11上的组件时出现角度6错误

时间:2018-06-05 17:19:19

标签: javascript angular typescript internet-explorer

我在IE中收到以下错误:

  

SCRIPT5022:无法解析StorageService的所有参数:([object Object],?)。

是否只需提供window的垫片 angular应用程序声明StorageService如下:

@Injectable()
export class StorageService implements IStorageService {

    /**
     * @constructor create new storage service.
     * @param window the window.
     */
    constructor(
        private readonly logger: NGXLogger,
        private readonly window: Window) {
    }

    /**
     * clear the token with the supplied key.
     * @param key a string containing the key.
     */
    clear(token: string) {
        window.localStorage.removeItem("token");
    }

    /**
     * load a value using the supplied key.
     * @param key a string
     * @returns {T} the stored value, or null;
     */
    load<T>(key: string): T {
        try {
            const value = window.localStorage.getItem(key);
            if (value === undefined || value === null || value === "undefined")
                return null;

            const instance = JSON.parse(value);
            return instance as T;
        } catch (exception) {
            this.logger.warn(`error obtaining key [${key}] :: ${exception}`);
            return null;
        }
    }

    /**
     * save the supplied value under the supplied key.
     * @param key the key.
     * @param value the value.
     * @returns {T} the supplied value
     */
    save<T>(key: string, value: T): T {
        window.localStorage.setItem(key, JSON.stringify(value));
        return value;
    }
}

0 个答案:

没有答案