在angular2

时间:2016-06-11 15:38:12

标签: angular

我已经创建了一些应该在整个应用程序中共享的服务,但由于某种原因,子组件会抛出错误 Error: DI Exception at NoProviderError.BaseException [as constructor] 我已经提供了启动文件中的所有服务

bootstrap(AppComponent, [
    JwtHelper,
    HTTP_PROVIDERS, authervice, ROUTER_PROVIDERS, 
    categoryService,
    Configuration
]);

任何人都可以提出建议吗?如果您需要更多解释或代码,请告诉我。

类别组件

import ...
import {categoryService}       from './categoryService';
@Component({
    selector: 'category-list',
    template: `...`,
    directives: [],
    styles: ['.error {color:red;}'],
    providers: []

})
export class categoryComponent implements OnInit {
    constructor(private _categoryService: categoryService) { }
    ...
}

如果我在上面的提供程序中添加categoryservice,则无法正常抛出错误

完成错误

  

EXCEPTION:错误:未捕获(在承诺中):EXCEPTION:错误:0:0   原始例外:没有categoryService的提供者!原版的   STACKTRACE:错误:DI异常       在NoProviderError.BaseException [作为构造函数](http://localhost:58056/libs/@angular/core/src/facade/exceptions.js:17:23)       在NoProviderError.AbstractProviderError [作为构造函数](http://localhost:58056/libs/@angular/core/src/di/reflective_exceptions.js:39:16)       在新的NoProviderError(http://localhost:58056/libs/@angular/core/src/di/reflective_exceptions.js:75:16)       在ReflectiveInjector _._ throwOrNull(http://localhost:58056/libs/@angular/core/src/di/reflective_injector.js:776:19)       在ReflectiveInjector _._ getByKeyDefault(http://localhost:58056/libs/@angular/core/src/di/reflective_injector.js:804:25)       在ReflectiveInjector _._ getByKey(http://localhost:58056/libs/@angular/core/src/di/reflective_injector.js:767:25)       在ReflectiveInjector_.get(http://localhost:58056/libs/@angular/core/src/di/reflective_injector.js:576:21)       在ElementInjector.get(http://localhost:58056/libs/@angular/core/src/linker/element_injector.js:23:48)       在ElementInjector.get(http://localhost:58056/libs/@angular/core/src/linker/element_injector.js:23:48)       在ReflectiveInjector _._ getByKeyDefault(http://localhost:58056/libs/@angular/core/src/di/reflective_injector.js:801:24)   错误上下文:[object Object] zone.js:461未处理的Promise   rejection:EXCEPTION:错误:0:0原始异常:没有提供者   对于categoryService! ORIGINAL STACKTRACE:错误:DI异常       在NoProviderError.BaseException [作为构造函数](http://localhost:58056/libs/@angular/core/src/facade/exceptions.js:17:23)       在NoProviderError.AbstractProviderError [作为构造函数](http://localhost:58056/libs/@angular/core/src/di/reflective_exceptions.js:39:16)       在新的NoProviderError(http://localhost:58056/libs/@angular/core/src/di/reflective_exceptions.js:75:16)       在ReflectiveInjector _._ throwOrNull(http://localhost:58056/libs/@angular/core/src/di/reflective_injector.js:776:19)       在ReflectiveInjector _._ getByKeyDefault(http://localhost:58056/libs/@angular/core/src/di/reflective_injector.js:804:25)       在ReflectiveInjector _._ getByKey(http://localhost:58056/libs/@angular/core/src/di/reflective_injector.js:767:25)       在ReflectiveInjector_.get(http://localhost:58056/libs/@angular/core/src/di/reflective_injector.js:576:21)       在ElementInjector.get(http://localhost:58056/libs/@angular/core/src/linker/element_injector.js:23:48)       在ElementInjector.get(http://localhost:58056/libs/@angular/core/src/linker/element_injector.js:23:48)       在ReflectiveInjector _._ getByKeyDefault(http://localhost:58056/libs/@angular/core/src/di/reflective_injector.js:801:24)   错误上下文:[object Object];区域:角;任务:Promise.then;   值:ViewWrappedException {_wrapperMessage:“错误:0:0”,   _originalException:NoProviderError,_originalStack:“错误:DI异常处于   NoProviderError.BaseExc ... ngular /核心/ src目录/ DI / reflective_injector.js:801:24)”,   _context:DebugContext,_wrapperStack:“错误:错误:0:0↵在ViewWrappedException.W ... localhost:58056 / libs / zone.js / dist / zone.js:322:35)”} consoleError   @ zone.js:461_loop_1 @ zone.js:490drainMicroTaskQueue @   zone.js:494ZoneTask.invoke @ zone.js:426 zone.js:463错误:未捕获   (承诺):EXCEPTION:错误:0:0原始例外:没有提供者   对于categoryService! ORIGINAL STACKTRACE:错误:DI异常       在NoProviderError.BaseException [作为构造函数](http://localhost:58056/libs/@angular/core/src/facade/exceptions.js:17:23)       在NoProviderError.AbstractProviderError [作为构造函数](http://localhost:58056/libs/@angular/core/src/di/reflective_exceptions.js:39:16)       在新的NoProviderError(http://localhost:58056/libs/@angular/core/src/di/reflective_exceptions.js:75:16)       在ReflectiveInjector _._ throwOrNull(http://localhost:58056/libs/@angular/core/src/di/reflective_injector.js:776:19)       在ReflectiveInjector _._ getByKeyDefault(http://localhost:58056/libs/@angular/core/src/di/reflective_injector.js:804:25)       在ReflectiveInjector _._ getByKey(http://localhost:58056/libs/@angular/core/src/di/reflective_injector.js:767:25)       在ReflectiveInjector_.get(http://localhost:58056/libs/@angular/core/src/di/reflective_injector.js:576:21)       在ElementInjector.get(http://localhost:58056/libs/@angular/core/src/linker/element_injector.js:23:48)       在ElementInjector.get(http://localhost:58056/libs/@angular/core/src/linker/element_injector.js:23:48)       在ReflectiveInjector _._ getByKeyDefault(http://localhost:58056/libs/@angular/core/src/di/reflective_injector.js:801:24)   错误上下文:[object Object]       在NoProviderError.BaseException [作为构造函数](http://localhost:58056/libs/@angular/core/src/facade/exceptions.js:17:23)       在NoProviderError.AbstractProviderError [作为构造函数](http://localhost:58056/libs/@angular/core/src/di/reflective_exceptions.js:39:16)       在新的NoProviderError(http://localhost:58056/libs/@angular/core/src/di/reflective_exceptions.js:75:16)       在ReflectiveInjector _._ throwOrNull(http://localhost:58056/libs/@angular/core/src/di/reflective_injector.js:776:19)       在ReflectiveInjector _._ getByKeyDefault(http://localhost:58056/libs/@angular/core/src/di/reflective_injector.js:804:25)       在ReflectiveInjector _._ getByKey(http://localhost:58056/libs/@angular/core/src/di/reflective_injector.js:767:25)       在ReflectiveInjector_.get(http://localhost:58056/libs/@angular/core/src/di/reflective_injector.js:576:21)       在ElementInjector.get(http://localhost:58056/libs/@angular/core/src/linker/element_injector.js:23:48)       在ElementInjector.get(http://localhost:58056/libs/@angular/core/src/linker/element_injector.js:23:48)       在ReflectiveInjector _._ getByKeyDefault(http://localhost:58056/libs/@angular/core/src/di/reflective_injector.js:801:24)   错误上下文:[object Object]       at resolvePromise(http://localhost:58056/libs/zone.js/dist/zone.js:538:32)       在http://localhost:58056/libs/zone.js/dist/zone.js:574:18       在ZoneDelegate.invokeTask(http://localhost:58056/libs/zone.js/dist/zone.js:356:38)       at Object.NgZoneImpl.inner.inner.fork.onInvokeTask(http://localhost:58056/libs/@angular/core/src/zone/ng_zone_impl.js:36:41)       在ZoneDelegate.invokeTask(http://localhost:58056/libs/zone.js/dist/zone.js:355:43)       在Zone.runTask(http://localhost:58056/libs/zone.js/dist/zone.js:256:48)       在drainMicroTaskQueue(http://localhost:58056/libs/zone.js/dist/zone.js:474:36)       在XMLHttpRequest.ZoneTask.invoke(http://localhost:58056/libs/zone.js/dist/zone.js:426:22)consoleError   @ zone.js:463_loop_1 @ zone.js:490drainMicroTaskQueue @   zone.js:494ZoneTask.invoke @ zone.js:426

2 个答案:

答案 0 :(得分:5)

SystemJs 区分大小写(按设计)。如果在项目文件中使用不同的路径名,请执行以下操作:

<强> main.ts

import { categoryService } from './categoryService';

<强> 类别-component.ts

import { categoryService } from './categoryservice';

然后系统js将进行双重导入 enter image description here

这样,angular2将在提供者Map键中找到服务对象的其他实例。

enter image description here

尽管Map对象中存在键。

enter image description here

地图的

has方法将返回false。这就是你收到错误的原因。

请参阅此页面https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Map#Key_equality

中有关Map对象中关键字相等性的更多信息

答案 1 :(得分:1)

我认为您在引导应用程序时以正确的方式指定提供程序。

您可能无法在引导模块中正确导入您的服务。您可以在此处检查categoryService是否未定义:

import { categoryService } from './something';

console.log(categoryService); // <-----

bootstrap(AppComponent, [
  JwtHelper,
  HTTP_PROVIDERS, authervice, ROUTER_PROVIDERS, 
  categoryService,
  Configuration
]);