服务实例不适用于angular2中的子组件

前端之家收集整理的这篇文章主要介绍了服务实例不适用于angular2中的子组件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经创建了一些应该在整个应用程序中共享的服务,但由于某些原因,子组件会抛出错误
错误:NoProviderError.BaseException [作为构造函数]的DI异常
我在启动文件中提供了所有服务
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: Error: Uncaught (in promise): EXCEPTION: Error in :0:0
ORIGINAL EXCEPTION: No provider for categoryService! ORIGINAL
STACKTRACE: Error: DI Exception
at NoProviderError.BaseException [as constructor] (07000)
at NoProviderError.AbstractProviderError [as constructor] (07001)
at new NoProviderError (07002)
at ReflectiveInjector_._throwOrNull (07003)
at ReflectiveInjector_._getByKeyDefault (07004)
at ReflectiveInjector_._getByKey (07005)
at ReflectiveInjector_.get (07006)
at ElementInjector.get (07007)
at ElementInjector.get (07007)
at ReflectiveInjector_._getByKeyDefault (07009)
ERROR CONTEXT: [object Object] zone.js:461 Unhandled Promise
rejection: EXCEPTION: Error in :0:0 ORIGINAL EXCEPTION: No provider
for categoryService! ORIGINAL STACKTRACE: Error: DI Exception
at NoProviderError.BaseException [as constructor] (07000)
at NoProviderError.AbstractProviderError [as constructor] (07001)
at new NoProviderError (07002)
at ReflectiveInjector_._throwOrNull (07003)
at ReflectiveInjector_._getByKeyDefault (07004)
at ReflectiveInjector_._getByKey (07005)
at ReflectiveInjector_.get (07006)
at ElementInjector.get (07007)
at ElementInjector.get (07007)
at ReflectiveInjector_._getByKeyDefault (07009)
ERROR CONTEXT: [object Object] ; Zone: angular ; Task: Promise.then ;
Value: ViewWrappedException {_wrapperMessage: “Error in :0:0”,
_originalException: NoProviderError,_originalStack: “Error: DI Exception↵ at
NoProviderError.BaseExc…ngular/core/src/di/reflective_injector.js:801:24)”,
_context: DebugContext,_wrapperStack: “Error: Error in :0:0↵ at 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 Error: Uncaught
(in promise): EXCEPTION: Error in :0:0 ORIGINAL EXCEPTION: No provider
for categoryService! ORIGINAL STACKTRACE: Error: DI Exception
at NoProviderError.BaseException [as constructor] (07000)
at NoProviderError.AbstractProviderError [as constructor] (07001)
at new NoProviderError (07002)
at ReflectiveInjector_._throwOrNull (07003)
at ReflectiveInjector_._getByKeyDefault (07004)
at ReflectiveInjector_._getByKey (07005)
at ReflectiveInjector_.get (07006)
at ElementInjector.get (07007)
at ElementInjector.get (07007)
at ReflectiveInjector_._getByKeyDefault (07009)
ERROR CONTEXT: [object Object]
at NoProviderError.BaseException [as constructor] (07000)
at NoProviderError.AbstractProviderError [as constructor] (07001)
at new NoProviderError (07002)
at ReflectiveInjector_._throwOrNull (07003)
at ReflectiveInjector_._getByKeyDefault (07004)
at ReflectiveInjector_._getByKey (07005)
at ReflectiveInjector_.get (07006)
at ElementInjector.get (07007)
at ElementInjector.get (07007)
at ReflectiveInjector_._getByKeyDefault (07009)
ERROR CONTEXT: [object Object]
at resolvePromise (070040)
at 070041
at ZoneDelegate.invokeTask (070042)
at Object.NgZoneImpl.inner.inner.fork.onInvokeTask (070043)
at ZoneDelegate.invokeTask (070044)
at Zone.runTask (070045)
at drainMicroTaskQueue (070046)
at XMLHttpRequest.ZoneTask.invoke (070047
@ zone.js:463_loop_1 @ zone.js:490drainMicroTaskQueue @
zone.js:494ZoneTask.invoke @ zone.js:426

SystemJs区分大小写(按设计).如果在项目文件中使用不同的路径名,如下所示:

main.ts

import { categoryService } from './categoryService';

品类component.ts

import { categoryService } from './categoryservice';

然后系统js将进行双重导入

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

尽管键对象存在于Map对象中.

有Map的方法会返回false.这就是你收到错误的原因.

有关Map对象中的键相等性的更多信息,请参见本页https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Map#Key_equality

猜你在找的Angularjs相关文章