gpt4 book ai didi

angularjs - 当 .module 定义中包含 localStorage 时,为什么我不能在 .config 中使用它?

转载 作者:行者123 更新时间:2023-12-04 14:01:40 25 4
gpt4 key购买 nike

我有以下内容:

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');

这给了我一个错误:
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)

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

最佳答案

只有提供者和常量可以注入(inject)到配置 block 中。请参阅 AngularJs 文档了解更多信息:http://docs.angularjs.org/guide/module (sektion“模块加载和依赖”)。

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.

关于angularjs - 当 .module 定义中包含 localStorage 时,为什么我不能在 .config 中使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18944268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com