StaticInjectorError,NullInjectorError,没有服务提供者

时间:2018-03-29 20:40:14

标签: angular

我想出了所有的想法。无论我用什么模块来提供我的服务,我都会遇到同样的错误。这显示在根AppModule中。我认为它应该具有全球范围,所以我不应该把它放在哪里,对吗?

这是app.module.ts:

@NgModule({
  imports: [
    BrowserModule,
    RouterModule,
  ],
  exports: [
    ReactiveFormsModule,
    FormsModule
  ],
  declarations: [
    AppComponent,
  ],
  providers: [
    SectionPagingService,
  ],
  bootstrap: [ AppComponent ]
})

export class AppModule {}

部分-paging.service.ts:

@Injectable()
export class SectionPagingService {

  sectionRoute: string;
  index: number;
  routesByOrder: string[] = [];
  applicationBaseRoute: string;
  url: string;

  constructor(private router: Router, private formValidityService: FormValidityService) {
    router.events.subscribe(event => this.getRouteIndex(event));
  }

  get formValid(): boolean {
    return this.formValidityService.formValid;
  }

  getRouteIndex(event: any) {

    if (event instanceof NavigationEnd || !event) {

      const evt: RouterEvent = (event) ? <RouterEvent>event : null;

      this.url = (evt) ? evt.url : this.url;
      const tree: UrlTree = this.router.parseUrl(this.url);
      const g: UrlSegmentGroup = tree.root.children[PRIMARY_OUTLET];
      const s: UrlSegment[] = g.segments;

      this.applicationBaseRoute = '';
      for (let j = 0; j < 3; j++) {
        this.applicationBaseRoute += '/' + s[j];
      }

      if (s[1].path === 'application') {

        this.sectionRoute = s[3].path;
        if (s[4]) { this.sectionRoute += '/' + s[4].path };

        if (this.sectionRoute === 'phs') {
          this.sectionRoute = 'phs/personal';
        }

        if (this.sectionRoute === 'juneau-forms') {
          this.sectionRoute = 'juneau-forms/basic-training';
        }

        this.index = this.routesByOrder.indexOf(this.sectionRoute);
      }
    }
  }

  init() {

    setTimeout(() => {

      const routes = (this.applicationBaseRoute.indexOf('applicant') >= 0) ?
        applicantApplicationRoutes[0] :
        agencyApplicationRoutes[0];

      routes.children.forEach((val: Route) => {
        if (typeof val.children === 'undefined') {
          this.routesByOrder.push(val.path);
        } else {
          val.children.forEach((val2: Route) => {
            if (val2.path !== '') {
              const path: string = val.path + '/' + val2.path;
              this.routesByOrder.push(path);
            }
          });
        }
      });

      this.getRouteIndex(null);

    });
  }

  prev(): void {
    if (this.index === 0) {
      this.index = this.routesByOrder.length - 1;
    } else {
      this.index--;
    }
    const route = this.routesByOrder[this.index];
    this.router.navigateByUrl(this.applicationBaseRoute + '/' + route);
  }

  next(): void {
    if (this.index === this.routesByOrder.length) {
      this.index = 0;
    } else {
      this.index++;
    }
    const route = this.routesByOrder[this.index];
    this.router.navigateByUrl(this.applicationBaseRoute + '/' + route);
  }

}

错误:

core.js:1448 ERROR Error: Uncaught (in promise): Error: StaticInjectorError(AppModule)[ApplicationComponent -> SectionPagingService]: 
  StaticInjectorError(Platform: core)[ApplicationComponent -> SectionPagingService]: 
    NullInjectorError: No provider for SectionPagingService!
Error: StaticInjectorError(AppModule)[ApplicationComponent -> SectionPagingService]: 
  StaticInjectorError(Platform: core)[ApplicationComponent -> SectionPagingService]: 
    NullInjectorError: No provider for SectionPagingService!
    at _NullInjector.get (core.js:1002)
    at resolveToken (core.js:1300)
    at tryResolveToken (core.js:1242)
    at StaticInjector.get (core.js:1110)
    at resolveToken (core.js:1300)
    at tryResolveToken (core.js:1242)
    at StaticInjector.get (core.js:1110)
    at resolveNgModuleDep (core.js:10854)
    at NgModuleRef_.get (core.js:12087)
    at resolveDep (core.js:12577)
    at _NullInjector.get (core.js:1002)
    at resolveToken (core.js:1300)
    at tryResolveToken (core.js:1242)
    at StaticInjector.get (core.js:1110)
    at resolveToken (core.js:1300)
    at tryResolveToken (core.js:1242)
    at StaticInjector.get (core.js:1110)
    at resolveNgModuleDep (core.js:10854)
    at NgModuleRef_.get (core.js:12087)
    at resolveDep (core.js:12577)
    at resolvePromise (zone.js:809)
    at resolvePromise (zone.js:775)
    at eval (zone.js:858)
    at ZoneDelegate.invokeTask (zone.js:421)
    at Object.onInvokeTask (core.js:4740)
    at ZoneDelegate.invokeTask (zone.js:420)
    at Zone.runTask (zone.js:188)
    at drainMicroTaskQueue (zone.js:595)
    at ZoneTask.invokeTask [as invoke] (zone.js:500)
    at invokeTask (zone.js:1517)

我整个上午一直在敲打着我。我不知道为什么提供服务会如此困难。

0 个答案:

没有答案