Angular 4:SharedModule导致错误

时间:2017-11-28 13:24:31

标签: javascript angular angular-module

我的 sharedModule 有一项名为 ConfigService 的服务,并使用 forRoot()模式:

shared.module.ts:

import { NgModule, ModuleWithProviders } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ConfigService } from './config.service';// resolve ENV variable

@NgModule({
  imports: [
    CommonModule
  ],
  declarations: [],
  providers:[]
})
export class SharedModule {
  static forRoot():ModuleWithProviders{
    return{
      ngModule:SharedModule,
      providers:[ConfigService]
    }
  }
 }

app.module.ts

@NgModule({
  declarations: [
    AppComponent,
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpClientModule,
    SharedModule.forRoot(),
    AuthModule.forRoot()
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

当我试图在另一个模块服务中注入ConfigService时 (AuthModule - > AuthService) - 我收到以下错误

Uncaught TypeError: Cannot read property 'forChild' of undefined
    at Object.cDNt (main.d647daef485a4cf9cf6d.bundle.js:1)
    at n (inline.7c624a7372e785c7b8fb.bundle.js:1)
    at Object.0 (main.d647daef485a4cf9cf6d.bundle.js:1)
    at n (inline.7c624a7372e785c7b8fb.bundle.js:1)
    at window.webpackJsonp (inline.7c624a7372e785c7b8fb.bundle.js:1)
    at main.d647daef485a4cf9cf6d.bundle.js:1

AuthService.ts:

@Injectable()
export class AuthService {
  constructor(private http: HttpClient, private ConfigService:ConfigService) { 

  }

我无法解决导致此错误的原因。

0 个答案:

没有答案