Angular 6 App在ng build上运行良好。
但是在ng build --prod app UI被加载但是在登录时我将从服务器获取coorect数据,之后我通过代码this.router.navigate(['/dashboard']);
导航到仪表板页面,我得到如下错误
错误错误:未捕获(在承诺中):错误:StaticInjectorError(a)[t]:StaticInjectorError(Platform:core)[t]:NullInjectorError:没有t的提供者!错误:StaticInjectorError(a)[t]:StaticInjectorError(Platform:core)[t]:NullInjectorError:没有t的提供者! at t.get(main.aa8ef128615c160bd861.js:1)at main.aa8ef128615c160bd861.js:1 at t(main.aa8ef128615c160bd861.js:1)at t.get(main.aa8ef128615c160bd861.js:1)at main.aa8ef128615c160bd861。 js:1 at t(main.aa8ef128615c160bd861.js:1)at t.get(main.aa8ef128615c160bd861.js:1)at at(main.aa8ef128615c160bd861.js:1)at t.get(main.aa8ef128615c160bd861.js:1 )at t.getToken(main.aa8ef128615c160bd861.js:1)at t.get(main.aa8ef128615c160bd861.js:1)at main.aa8ef128615c160bd861.js:1 at t(main.aa8ef128615c160bd861.js:1)at t.get (main.aa8ef128615c160bd861.js:1)at main.aa8ef128615c160bd861.js:1 at t(main.aa8ef128615c160bd861.js:1)at t.get(main.aa8ef128615c160bd861.js:1)at Da(main.aa8ef128615c160bd861.js: 1)at t.get(main.aa8ef128615c160bd861.js:1)at t.getToken(main.aa8ef128615c160bd861.js:1)at j(polyfills.9a5f6d04e0781d28c53e.js:1)at j(polyfills.9a5f6d04e0781d28c53e.js:1) at polyfills.9a5f6d04e0781d28c53e.js:1 at e.invokeTask(polyfills.9a5f6d04e0781d28c53e.js:1)at Object.onInvokeTask(m ain.aa8ef128615c160bd861.js:1)at e.invokeTask(polyfills.9a5f6d04e0781d28c53e.js:1)at t.runTask(polyfills.9a5f6d04e0781d28c53e.js:1)at d(polyfills.9a5f6d04e0781d28c53e.js:1)at t.invokeTask [作为调用](polyfills.9a5f6d04e0781d28c53e.js:1)at _(polyfills.9a5f6d04e0781d28c53e.js:1)
我的App.module.ts看起来像,
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { BsDatepickerModule } from 'ngx-bootstrap';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { HttpClientModule } from '@angular/common/http';
import { ChartsModule } from 'ng2-charts';
import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
import { MaterialModule } from './angular-material/material.module';
import { DashboardComponent } from './dashboard/dashboard.component';
import { AppRoutingModule } from './app.routing-module';
import { FiltersComponent } from './filters/filters.component';
import { SearchComponent } from './search/search.component';
import { ChartComponent } from './chart/chart.component';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { KeysPipe } from './shared/pipes/keys.pipe';
import { PdfViewerModule } from 'ng2-pdf-viewer';
import { AboutComponent } from './about/about.component';
@NgModule({
declarations: [
AppComponent,
HomeComponent,
DashboardComponent,
FiltersComponent,
SearchComponent,
ChartComponent,
KeysPipe,
AboutComponent
],
imports: [
BrowserModule,
BrowserAnimationsModule,
FormsModule,
ReactiveFormsModule,
MaterialModule,
ChartsModule,
HttpClientModule,
BsDatepickerModule.forRoot(),
AppRoutingModule,
PdfViewerModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
应用环境是: -
Angular CLI: 6.0.8
Node: 10.2.1
OS: linux x64
Angular: 6.0.4
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router
- 0.6.8, .6.8, .6.8, .6.8, -0.6.8, @angular/cdk -6.2.1, @angular/cli -6.0.8, @angular/material - 6.2.1, - 6.0.8, .6.8, - 0.6.8 , rxjs-6.2.1, typescript -2.7.2, webpack -4.8.3
答案 0 :(得分:1)
您是否正在使用任何服务从服务器获取数据?因为我看不到您的应用模块中添加的任何服务。请检查这可能是一个问题。
答案 1 :(得分:1)
问题是,
我在app-routing.module.ts文件中使用了CanActivateChild authgaurd服务, 应该在app.module.ts提供程序数组中提供此服务,我错过了它。
如果app模块文件中的providers数组中的服务工作正常。
但是在dev(ng build)构建中,它并没有给我任何错误。
答案 2 :(得分:0)
我在一个组件中缺少提供程序时遇到了同样的问题,为简化调试,我建议以用户的身份进行操作[Milan Velebit在这里建议] [1]
通过进入angular.json文件并编辑production:对象来启用详细的输出,以便:“ aot”:true,“ optimization”:false,“ buildOptimizer”:false。
这消除了构建过程中的丑陋,因此错误日志中包含缺少的模块名称