简体   繁体   English

Angular2“t 没有提供者!” 和未捕获(承诺):错误:DI 错误

[英]Angular2 "No provider for t!" and Uncaught (in promise): Error: DI Error

I've build a app in Angular 2, and have encountered a issue.我已经在 Angular 2 中构建了一个应用程序,但遇到了一个问题。 I have used angular CLI to create my application, I have created my components and services using Angular CLI and I use " ng serve " to run the application locally and everything works fine.我使用 angular CLI 创建我的应用程序,我使用 Angular CLI 创建了我的组件和服务,我使用“ ng serve ”在本地运行应用程序,一切正常。 In order to create a build for PROD I'm using the command " ng build --Prod --base-href ./ --aot ", it creates a DIST folder and hosting the folder in IIS opens the application fine.为了为 PROD 创建构建,我使用命令“ ng build --Prod --base-href ./ --aot ”,它创建了一个 DIST 文件夹,并在 IIS 中托管该文件夹可以很好地打开应用程序。 When I check in the code to TFS there is a event which automatically creates DIST folder using jenkins and pushes the build to my server.当我将代码签入 TFS 时,有一个事件会使用 jenkins 自动创建 DIST 文件夹并将构建推送到我的服务器。 Now if I browse the application for server it throws the following errors " No provider for t! " and Error: DI Error.现在,如果我浏览服务器的应用程序,它会抛出以下错误“ No provider for t! ”和错误:DI 错误。 I don't know what I'm doing wrong here.我不知道我在这里做错了什么。

Below are the screenshots of the error下面是错误截图

在此处输入图片说明

在此处输入图片说明


Any help is much appreciated.任何帮助深表感谢。

I was having the same problem.我遇到了同样的问题。 I was able to solve it by:我能够通过以下方式解决它:

  1. running ng serve -aot .运行ng serve -aot I was able to find out what the missing class was as this is running AOT without running minify/uglify.我能够找出丢失的类是什么,因为它正在运行 AOT 而不运行 minify/uglify。
  2. Once I did that I was able to look through the error context to figure out which component was having issues.一旦我这样做了,我就能够查看错误上下文以找出哪个组件有问题。
  3. Go to the component in question and start removing references in the constructor (where the dependency resolution happens.转到有问题的组件并开始删除构造函数中的引用(发生依赖项解析的地方。

In my case, I was having a problem using the Logger class in the angular2-logger/core module.就我而言,我在使用 angular2-logger/core 模块中的 Logger 类时遇到了问题。 Once I removed the reference from the constructor and reran compilation, it worked like a charm.一旦我从构造函数中删除了引用并重新编译,它就像一个魅力。 I then tested with ng serve -prod to confirm that was also fixed.然后我用ng serve -prod进行了测试以确认它也被修复了。

One thing I found was that I had to explicitly kill Angular CLI and rebuild.我发现的一件事是我必须明确杀死 Angular CLI 并重建。 For some reason, when I modified one line and saved, it said it recompiled and was still broken.不知什么原因,我修改一行并保存时,它说它重新编译并仍然损坏。 Only when I killed it and ran the command again did I figure out it was really fixed.只有当我杀死它并再次运行命令时,我才发现它真的修复了。

I had the same problem and the cause turned out to be a quirk in the angular2-logger library.我遇到了同样的问题,结果证明是 angular2-logger 库中的一个怪癖。 This answer solved the problem for me 这个答案为我解决了问题

fixed !固定的 ! in my case it was because the NotificationService for the angular2-notifications就我而言,这是因为angular2-notificationsNotificationService

the solution is just remove the NotificationService from the providers array inside you app module .解决方案只是从app 模块内的 providers 数组中删除NotificationService

but still this is should be validated earlier by angular in AOT mode .但这仍然应该在 AOT 模式下通过 angular 提前验证。

Thanks!谢谢!

Here's one way to pinpoint the error - start by disabling the uglify plugin (which is automatically set for production) - and there's no way to do this with angular-cli.这是查明错误的一种方法- 首先禁用 uglify 插件(自动设置用于生产) - 而 angular-cli 无法做到这一点。 It can be temporarily commented out by searching for UglifyJSPlugin in node_modules directory.可以通过在 node_modules 目录中搜索 UglifyJSPlugin 暂时注释掉。 After finding it (in the common.js file), comment it out找到后(在common.js文件中),注释掉

/*              
// Temporarily comment out this part (in common.js of node_modules) to pinpoint the error
                new UglifyJSPlugin({
                    sourceMap: buildOptions.sourceMap,
                    parallel: true,
                    cache: true,
                    uglifyOptions,
                }),
              */

Build your app again and run.再次构建您的应用程序并运行。 Then, a cryptic as heck error like this one然后,像这样一个神秘的错误

Unhandled Promise rejection: StaticInjectorError(ne)[t -> t]: 
  StaticInjectorError(Platform: core)[t -> t]: 
    NullInjectorError: No provider for t! ; Zone: <root> ; Task: Promise.then ; Value: Error: StaticInjectorError(ne)[t -> t]: 
  StaticInjectorError(Platform: core)[t -> t]: 
    NullInjectorError: No provider for t!

becomes变成

Unhandled Promise rejection: StaticInjectorError(AppModule)[TranslatorService -> Http]: 
  StaticInjectorError(Platform: core)[TranslatorService -> Http]: 
    NullInjectorError: No provider for Http! ; Zone: <root> ; Task: Promise.then ; Value: Error: StaticInjectorError(AppModule)[TranslatorService -> Http]: 
  StaticInjectorError(Platform: core)[TranslatorService -> Http]: 
    NullInjectorError: No provider for Http!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 2 No Provider或DI错误(未捕获(承诺):错误:No Provider…) - Angular 2 No Provider or DI error (Uncaught (in promise): Error: No provider …) '错误:未捕获(承诺):没有Jsonp的提供者'(HTML / Javascript / Typescript / Angular2) - 'Error: Uncaught (in promise): No provider for Jsonp' (HTML / Javascript / Typescript / Angular2) Angular2 DI 错误 EventEmitter 没有提供者 - Angular2 DI Error No provider for EventEmitter Angular2组件中的DI错误 - DI Error in Angular2 component 错误:未捕获(承诺):错误:没有服务提供程序-Angular 4.1.2 - Error: Uncaught (in promise): Error: No provider for Service - Angular 4.1.2 angular 4错误:未捕获(承诺):错误:没有ConnectionBackend的提供程序! 在注入Jsonp时 - angular 4 Error: Uncaught (in promise): Error: No provider for ConnectionBackend! while injecting Jsonp 使用Karma的Angular测试中的错误:未捕获(承诺):错误:没有SessionUtil的提供程序 - Error in Angular Test using Karma: Uncaught (in promise): Error: No provider for SessionUtil 将http定义为DI会给出EXCEPTION:未捕获(按承诺):错误:DI错误 - defining http as DI gives EXCEPTION: Uncaught (in promise): Error: DI Error 未捕获(承诺):错误:没有 GooglePlus 的提供者 - Uncaught (in promise): Error: No provider for GooglePlus Angular2:如何调试“未捕获的无效提供程序”错误? - Angular2: HowTo debug “Uncaught Invalid Provider” Error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM