Error: No provider for t
Error: No provider for t
我正在尝试通过 ng build --prod 将我的 angular 代码编译到生产模式,它成功了,但是在浏览器中我得到了错误:
ERROR p {__zone_symbol__error: Error: Uncaught (in promise): Error: NullInjectorError: No provider for t!
错误:NullInjectorError:...,...}
当我 运行 只是 ng build 然后 html 工作正常。
我尝试了 git 中心提到的几个链接:
https://github.com/angular/angular/issues/19219
https://github.com/angular/angular-cli/issues/6851
帮助不大。
附上我的 Package.json 文件内容:
{
"name": "xxx",
"version": "0.0.0",
"license": "MIT",
"angular-cli": {},
"scripts": {
"ng": "ng",
"start": "ng serve",
"test": "ng test",
"pree2e": "webdriver-manager update --standalone false --gecko false",
"e2e": "protractor",
"build": "ng build && node server.js",
"dev": "ng build -w & nodemon server.js --watch dist --watch server",
"production": "ng build --prod & node server.js dist server "
},
"private": true,
"dependencies": {
"@angular/animations": "~5.0.0",
"@angular/common": "~5.0.0",
"@angular/compiler": "~5.0.0",
"@angular/compiler-cli": "~5.0.0",
"@angular/core": "~5.0.0",
"@angular/forms": "~5.0.0",
"@angular/http": "~5.0.0",
"@angular/platform-browser": "~5.0.0",
"@angular/platform-browser-dynamic": "~5.0.0",
"@angular/router": "~5.0.0",
"@jaspero/ng2-alerts": "0.0.7",
"axios": "^0.17.0",
"body-parser": "^1.18.2",
"cookie-parser": "^1.4.3",
"core-js": "^2.4.1",
"ethereumjs-tx": "^1.3.3",
"express": "^4.16.2",
"express-session": "^1.15.6",
"mongoose": "^4.12.6",
"rxjs": "^5.0.1",
"ts-helpers": "^1.1.1",
"web3": "^1.0.0-beta.24",
"web3-provider-engine": "^13.3.3",
"zone.js": "^0.7.2"
},
"devDependencies": {
"@angular/cli": "^1.5.0",
"@angular/compiler-cli": "1.5.0",
"@types/jasmine": "2.5.38",
"@types/node": "^6.0.42",
"codelyzer": "~2.0.0-beta.1",
"jasmine-core": "2.5.2",
"jasmine-spec-reporter": "2.5.0",
"karma": "1.2.0",
"karma-chrome-launcher": "^2.0.0",
"karma-cli": "^1.0.1",
"karma-jasmine": "^1.0.2",
"karma-remap-istanbul": "^0.2.1",
"protractor": "~4.0.13",
"ts-node": "1.2.1",
"tslint": "^4.3.0",
"typescript": "2.4.2"
}
}
还有app.module.ts
import {BrowserModule} from '@angular/platform-browser';
import {NgModule} from '@angular/core';
import {FormsModule} from '@angular/forms';
import {HttpClientModule} from '@angular/common/http';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {JasperoAlertsModule} from '@jaspero/ng2-alerts';
// Imports commented out for brevity
import {RouterModule} from '@angular/router';
// Components
import {AppComponent} from './app.component';
import {LoginComponent} from './component/login/login.component';
import {DashboardComponent} from './component/dashboard/dashboard.component';
// Services
import {RecordsService} from './services/records.service';
import {AccountService} from './services/account.service';
import {AuthenticateService} from './services/authenticate.service';
import {FooterComponent} from './component/footer/footer.component';
import {HeaderComponent} from './component/header/header.component';
// Define the routes
const ROUTES = [
{
path: '',
redirectTo: 'login',
pathMatch: 'full'
},
{
path: 'login',
component: LoginComponent
},
{
path: 'dashboard',
component: DashboardComponent
}
];
@NgModule({
declarations: [
AppComponent,
HeaderComponent,
FooterComponent,
LoginComponent,
DashboardComponent
],
imports: [
BrowserModule,
HttpClientModule,
BrowserAnimationsModule,
FormsModule,
JasperoAlertsModule,
RouterModule.forRoot(ROUTES) // Add routes to the app
],
providers: [
RecordsService,
AccountService,
AuthenticateService
],
bootstrap: [AppComponent]
})
export class AppModule {
}
您正在尝试使用未在您的 AppModule providers
中列出的服务。将服务添加到提供商列表以使其工作。
您尝试访问服务但未在您的模块中提供
通过编辑文件禁用 terser 插件:
node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/common.js
并注释掉:
extraMinimizers.push(new TerserPlugin({
sourceMap: scriptsSourceMap,
parallel: true,
cache: true,
terserOptions,
}));
使用 ng build frontend --prod
重新构建应用程序,您应该能够在控制台中看到无法注入的服务。
运行下面的命令,先找到缺少哪个服务依赖。
在本地修复它,Prod 构建将自动运行。
ng serve --prod --optimization=false
我不想放弃优化,因为包大小变得大约 8 倍。
幸运的是,它原来是一些愚蠢的东西 :) 我在我的一个组件中有 providers:[] 部分当我删除它时,错误消失了。
希望这可能对某人有所帮助!
对于错误:NullInjectorError:t 没有提供者! (注意:仅针对提供商为“t”的错误,如果它是另一个提供商,则可能是因为未声明 [=16=] -> 提供商 [])
问题出现在我用ng "ng build --prod"编译时,切换到"npm 运行 build --prod",问题消失了。
尝试使用其他版本或 npm 进行编译
我有这个“错误:NullInjectorError:t 没有提供者!”问题是因为我在 app.module
中导入 ReactiveFormsModule 之前使用了 FormBuilder
如果您在模块文件中遇到 导入 问题,
运行这段代码
ng serve --configuration=staging --optimization=false
要么
ng serve --configuration=prod--optimization=false
此代码将帮助您找到 Module 文件中未导入的 module。
就我而言,我错过了导入 MatSnackBarModule 和 BrowserModule
如果您在 app.module
中使用 HttpClient import HttpClientModule
.
.
.
imports: [
BrowserModule,
FormsModule,
AppRoutingModule,
HttpClientModule // <--- add this line
],
我的一个队友遇到过这个。他在 app.module.ts
imports
中失踪 HttpClientModule
我正在尝试通过 ng build --prod 将我的 angular 代码编译到生产模式,它成功了,但是在浏览器中我得到了错误:
ERROR p {__zone_symbol__error: Error: Uncaught (in promise): Error: NullInjectorError: No provider for t!
错误:NullInjectorError:...,...}
当我 运行 只是 ng build 然后 html 工作正常。
我尝试了 git 中心提到的几个链接:
https://github.com/angular/angular/issues/19219 https://github.com/angular/angular-cli/issues/6851
帮助不大。
附上我的 Package.json 文件内容:
{
"name": "xxx",
"version": "0.0.0",
"license": "MIT",
"angular-cli": {},
"scripts": {
"ng": "ng",
"start": "ng serve",
"test": "ng test",
"pree2e": "webdriver-manager update --standalone false --gecko false",
"e2e": "protractor",
"build": "ng build && node server.js",
"dev": "ng build -w & nodemon server.js --watch dist --watch server",
"production": "ng build --prod & node server.js dist server "
},
"private": true,
"dependencies": {
"@angular/animations": "~5.0.0",
"@angular/common": "~5.0.0",
"@angular/compiler": "~5.0.0",
"@angular/compiler-cli": "~5.0.0",
"@angular/core": "~5.0.0",
"@angular/forms": "~5.0.0",
"@angular/http": "~5.0.0",
"@angular/platform-browser": "~5.0.0",
"@angular/platform-browser-dynamic": "~5.0.0",
"@angular/router": "~5.0.0",
"@jaspero/ng2-alerts": "0.0.7",
"axios": "^0.17.0",
"body-parser": "^1.18.2",
"cookie-parser": "^1.4.3",
"core-js": "^2.4.1",
"ethereumjs-tx": "^1.3.3",
"express": "^4.16.2",
"express-session": "^1.15.6",
"mongoose": "^4.12.6",
"rxjs": "^5.0.1",
"ts-helpers": "^1.1.1",
"web3": "^1.0.0-beta.24",
"web3-provider-engine": "^13.3.3",
"zone.js": "^0.7.2"
},
"devDependencies": {
"@angular/cli": "^1.5.0",
"@angular/compiler-cli": "1.5.0",
"@types/jasmine": "2.5.38",
"@types/node": "^6.0.42",
"codelyzer": "~2.0.0-beta.1",
"jasmine-core": "2.5.2",
"jasmine-spec-reporter": "2.5.0",
"karma": "1.2.0",
"karma-chrome-launcher": "^2.0.0",
"karma-cli": "^1.0.1",
"karma-jasmine": "^1.0.2",
"karma-remap-istanbul": "^0.2.1",
"protractor": "~4.0.13",
"ts-node": "1.2.1",
"tslint": "^4.3.0",
"typescript": "2.4.2"
}
}
还有app.module.ts
import {BrowserModule} from '@angular/platform-browser';
import {NgModule} from '@angular/core';
import {FormsModule} from '@angular/forms';
import {HttpClientModule} from '@angular/common/http';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {JasperoAlertsModule} from '@jaspero/ng2-alerts';
// Imports commented out for brevity
import {RouterModule} from '@angular/router';
// Components
import {AppComponent} from './app.component';
import {LoginComponent} from './component/login/login.component';
import {DashboardComponent} from './component/dashboard/dashboard.component';
// Services
import {RecordsService} from './services/records.service';
import {AccountService} from './services/account.service';
import {AuthenticateService} from './services/authenticate.service';
import {FooterComponent} from './component/footer/footer.component';
import {HeaderComponent} from './component/header/header.component';
// Define the routes
const ROUTES = [
{
path: '',
redirectTo: 'login',
pathMatch: 'full'
},
{
path: 'login',
component: LoginComponent
},
{
path: 'dashboard',
component: DashboardComponent
}
];
@NgModule({
declarations: [
AppComponent,
HeaderComponent,
FooterComponent,
LoginComponent,
DashboardComponent
],
imports: [
BrowserModule,
HttpClientModule,
BrowserAnimationsModule,
FormsModule,
JasperoAlertsModule,
RouterModule.forRoot(ROUTES) // Add routes to the app
],
providers: [
RecordsService,
AccountService,
AuthenticateService
],
bootstrap: [AppComponent]
})
export class AppModule {
}
您正在尝试使用未在您的 AppModule providers
中列出的服务。将服务添加到提供商列表以使其工作。
您尝试访问服务但未在您的模块中提供
通过编辑文件禁用 terser 插件:
node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/common.js
并注释掉:
extraMinimizers.push(new TerserPlugin({
sourceMap: scriptsSourceMap,
parallel: true,
cache: true,
terserOptions,
}));
使用 ng build frontend --prod
重新构建应用程序,您应该能够在控制台中看到无法注入的服务。
运行下面的命令,先找到缺少哪个服务依赖。 在本地修复它,Prod 构建将自动运行。
ng serve --prod --optimization=false
我不想放弃优化,因为包大小变得大约 8 倍。
幸运的是,它原来是一些愚蠢的东西 :) 我在我的一个组件中有 providers:[] 部分当我删除它时,错误消失了。
希望这可能对某人有所帮助!
对于错误:NullInjectorError:t 没有提供者! (注意:仅针对提供商为“t”的错误,如果它是另一个提供商,则可能是因为未声明 [=16=] -> 提供商 [])
问题出现在我用ng "ng build --prod"编译时,切换到"npm 运行 build --prod",问题消失了。
尝试使用其他版本或 npm 进行编译
我有这个“错误:NullInjectorError:t 没有提供者!”问题是因为我在 app.module
中导入 ReactiveFormsModule 之前使用了 FormBuilder如果您在模块文件中遇到 导入 问题,
运行这段代码
ng serve --configuration=staging --optimization=false
要么
ng serve --configuration=prod--optimization=false
此代码将帮助您找到 Module 文件中未导入的 module。
就我而言,我错过了导入 MatSnackBarModule 和 BrowserModule
如果您在 app.module
.
.
.
imports: [
BrowserModule,
FormsModule,
AppRoutingModule,
HttpClientModule // <--- add this line
],
我的一个队友遇到过这个。他在 app.module.ts
imports
中失踪 HttpClientModule