加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 07:56:21 所属栏目:安全 来源:网络整理
导读:我已经创建了一些应该在整个应用程序中共享的服务,但由于某些原因,子组件会抛出错误 错误:NoProviderError.BaseException [作为构造函数]的DI异常 我在启动文件中提供了所有服务 bootstrap(AppComponent,[ JwtHelper,HTTP_PROVIDERS,authervice,ROUTER_PROV
我已经创建了一些应该在整个应用程序中共享的服务,但由于某些原因,子组件会抛出错误
错误: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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读