为什么我不能在.module定义中包含我的.config中使用localStorage?

时间:2021-09-23 00:40:48

I have the following:

我有以下内容:

var app = angular
    .module('app', ['ui.router', 'admin', 'home', 'questions', 'ngResource', 'LocalStorageModule'])
    .config(['$sceProvider', '$stateProvider', '$locationProvider', 'localStorageService',
        function ($sceProvider, $stateProvider, $locationProvider, localStorageService ) {

        $sceProvider.enabled(false);
        $locationProvider.html5Mode(true);

        localStorageService.add('adminContent', 'xx');

This is giving me an error:

这给了我一个错误:

Uncaught Error: [$injector:modulerr] Failed to instantiate module app due to:
Error: [$injector:unpr] Unknown provider: localStorageService
http://errors.angularjs.org/1.2.0-rc.2/$injector/unpr?p0=localStorageService
    at hasOwnPropertyFn (http://127.0.0.1:81/Scripts/angular-v1.2.0-rc.2.js:78:12)
    at http://127.0.0.1:81/Scripts/angular-v1.2.0-rc.2.js:2997:19
    at getService (http://127.0.0.1:81/Scripts/angular-v1.2.0-rc.2.js:3119:39)
    at Object.invoke (http://127.0.0.1:81/Scripts/angular-v1.2.0-rc.2.js:3140:13)
    at http://127.0.0.1:81/Scripts/angular-v1.2.0-rc.2.js:3078:37
    at Array.forEach (native)
    at forEach (http://127.0.0.1:81/Scripts/angular-v1.2.0-rc.2.js:224:11)
    at loadModules (http://127.0.0.1:81/Scripts/angular-v1.2.0-rc.2.js:3065:5)
    at createInjector (http://127.0.0.1:81/Scripts/angular-v1.2.0-rc.2.js:3007:11)
    at doBootstrap (http://127.0.0.1:81/Scripts/angular-v1.2.0-rc.2.js:1152:20)
http://errors.angularjs.org/1.2.0-rc.2/$injector/modulerr?p0=app&p1=Error%3…http%3A%2F%2F127.0.0.1%3A81%2FScripts%2Fangular-v1.2.0-rc.2.js%3A1152%3A20) 

Is it not possible to use localStorage in config like this? I have included the code for the localstorage module and it's loaded before the app.js. It works in other parts of the application, but the same line does not work when placed in the app.config

是不是可以在这样的配置中使用localStorage?我已经包含了localstorage模块的代码,并在app.js之前加载了它。它适用于应用程序的其他部分,但放在app.config中时,同一行不起作用

2 个解决方案

#1


8  

Only providers and constants are injectable in configuration blocks. See the AngularJs documentation for more insights: http://docs.angularjs.org/guide/module (sektion "Module Loading & Dependencies").

只有提供程序和常量可以在配置块中注入。有关更多见解,请参阅AngularJs文档:http://docs.angularjs.org/guide/module(sektion“Module Loading&Dependencies”)。

Configuration blocks - get executed during the provider registrations and configuration phase. Only providers and constants can be injected into configuration blocks. This is to prevent accidental instantiation of services before they have been fully configured.

配置块 - 在提供程序注册和配置阶段执行。只有提供程序和常量才能注入配置块。这是为了防止在完全配置服务之前意外实例化服务。

#2


1  

While the accepted answer correctly answers the question, it does not offer any solution (for Angular newbies looking for the right way to do this).

虽然接受的答案正确回答了问题,但它没有提供任何解决方案(对于Angular新手寻找正确的方法来做到这一点)。

You have access to services at run time, which happens after configuration. For instance:

您可以在运行时访问服务,这在配置后发生。例如:

angular.module('app').run(storeAdminContent);

storeAdminContent.$inject = ['localStorageService'];
function storeAdminContent(localStorageService) {
    localStorageService.add('adminContent', 'xx');
}

#1


8  

Only providers and constants are injectable in configuration blocks. See the AngularJs documentation for more insights: http://docs.angularjs.org/guide/module (sektion "Module Loading & Dependencies").

只有提供程序和常量可以在配置块中注入。有关更多见解,请参阅AngularJs文档:http://docs.angularjs.org/guide/module(sektion“Module Loading&Dependencies”)。

Configuration blocks - get executed during the provider registrations and configuration phase. Only providers and constants can be injected into configuration blocks. This is to prevent accidental instantiation of services before they have been fully configured.

配置块 - 在提供程序注册和配置阶段执行。只有提供程序和常量才能注入配置块。这是为了防止在完全配置服务之前意外实例化服务。

#2


1  

While the accepted answer correctly answers the question, it does not offer any solution (for Angular newbies looking for the right way to do this).

虽然接受的答案正确回答了问题,但它没有提供任何解决方案(对于Angular新手寻找正确的方法来做到这一点)。

You have access to services at run time, which happens after configuration. For instance:

您可以在运行时访问服务,这在配置后发生。例如:

angular.module('app').run(storeAdminContent);

storeAdminContent.$inject = ['localStorageService'];
function storeAdminContent(localStorageService) {
    localStorageService.add('adminContent', 'xx');
}