0

我正在尝试使用ngStorage将一些数据存储在我的localStorage. 但是我收到以下错误,这对我来说很奇怪:

错误:$injector:unpr 未知提供者 未知提供者:$localStorageProviderProvider <- $localStorageProvider

https://docs.angularjs.org/error/ $injector/unpr?p0=$localStorageProviderProvider%20%3C-%20$localStorageProvider

我也在使用打字稿。这是我的代码:

app.module.ts:

(() => {

    angular.module(App.Config.MODULE_NAME, [
        'ngRoute',
        'ui.bootstrap',
        'ngStorage',
        'toastr',
    ]);

})();

register.controller.ts:

namespace App.Components.Register {
"use strict";
import IUser = App.Interfaces.IUser;
import UserDataService = App.Services.Http.UserDataService;
import IStorageProvider = angular.storage.IStorageProvider;
import ILocationService = angular.ILocationService;
import LogService = App.Services.Util.LogService;

export class RegisterController extends BaseController {

    public user: IUser;

    public static $inject: string[] = ['$localStorageProvider', '$location', 'LogService', 'UserDataService'];

    constructor(private $localStorageProvider: IStorageProvider,
                private $location: ILocationService,
                private logService: LogService,
                private UserDataService: UserDataService) {
        super();
    }

    public submit() {
        this.UserDataService.create(this.user).then(
            (data) => {
                switch (data.status) {
                    case 1:
                        this.logService.success("Usuário criado com sucesso.");
                        this.$localStorageProvider.set("user", this.user);
                        this.$location.path("/");
                        break;
                    case -1:
                        this.logService.error("Erro na criação do usuário.");
                        this.user.password = null;
                        break;
                    case -2:
                        this.logService.warning("Nome de usuário já está sendo utilizado.");
                        this.user.username = null;
                        this.user.password = null;
                        break;
                    case -3:
                        this.logService.warning("Este e-mail já está sendo utilizado.");
                        this.user.email = null;
                        this.user.password = null;
                        break;
                }
            },
            (error) => {
                this.logService.error(error);
                this.user.password = null;
            }
        );
    }
}

angular.module(App.Config.MODULE_NAME).controller('RegisterController', RegisterController);
}

对我来说奇怪的是,如果我使用$localStorage而不是$localStorageProvider它不会引发任何错误,但我不会拥有get()/set()Provider 的功能。

对我来说奇怪的第二件事是 angular 生成的错误消息:

未知提供者:$localStorageProviderProvider <- $localStorageProvider

第二个 Provider 是$localStorageProviderProvider从哪里来的?

我知道我有解决方法,但是这个错误刚刚进入我的脑海,在我发现问题之前我不能让它消失。

提前致谢。

4

0 回答 0