将一项服务插入另一项服务时出错

时间:2018-09-20 19:18:51

标签: angular typescript

我创建了一项服务-ContactService

type contactPredicate = (contact: Contact) => boolean;
type contactLike = Contact | string | SelectedContact;

@Injectable()
export class ContactService {
  private selectedContactId: string = '';
  public selectedContactSubject: BehaviorSubject<contactLike>;

  public get SelectedContact(): contactLike {
    const contact: Contact = this.contactList.find((v) => v.Id === this.selectedContactId);

    return contact ? contact : null;
  }

  public set SelectedContact(value: contactLike) {
    this.selectedContactId = typeof value === 'string' ? value as string : value.Id;
    this.selectedContactSubject.next(this.findContact(this.selectedContactId));
  }

  constructor() {
    this.selectedContactSubject = new BehaviorSubject<Contact>(this.findContact(this.selectedContactId));
  }

}

当我将此服务插入另一个服务-“ FileService”

import { ContactService } from './contact.service';

@Injectable()
export class FileService {

  constructor(
    private httpServiceProvider: HttpServiceProvider,
    private userService: UserService,
    private contactService: ContactService) {
  }

  getFiles(fileType: string, contactType: ContactType, skip: number, pageSize: number, organisationId: string): Observable<any> {
    let myUserId = this.userService.userId;

    let selectedContact: SelectedContact;
    this.contactService.selectedContactSubject.subscribe(v => {
      selectedContact = (v as SelectedContact);
    });
    let selectedContactId = selectedContact.Id;
    let params: HttpParams = new HttpParams();

    params = params.set('category', fileType)
      .append('senderId', myUserId)
      .append('receiverId', selectedContactId)
      .append('Skip', skip.toString())
      .append('PageSize', pageSize.toString())
      .append('contactType', String(contactType))
      .append('organisationId', organisationId);

    return this.httpServiceProvider.httpGet(CONFIGURATION.GetFiles.url, params);
  }

}

SharedModule.ts

export function createConfig(): SignalRConfiguration
{
  const c = new SignalRConfiguration();
  c.hubName = CONFIGURATION.apiHub.name;
  c.url = CONFIGURATION.apiBaseUrl.server;

  return c;
}

const declarations: Array<any> = [
  ...
];

@NgModule({
  imports: [
    CommonModule,
    SignalRModule.forRoot(createConfig),
    SharedModule,
    InfiniteScrollModule
  ],
  declarations: declarations,
  providers: [
    SignalRService,
    HeaderService,
    ContactService,
    MessageFlaggingService,
    DataShareService,
    ConfirmationService,
    ContactsMetaData,
    DateTimeUtility,
    FileService
  ],
  exports: declarations.concat([SharedModule])
})
export class DashboardSharedModule { }

AppModule.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app.routing';

import { AppComponent } from './app.component';
import { CoreModule } from './core/core.module';
import { DashboardSharedModule } from './dashboard/shared/shared.module';


@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    AppRoutingModule,
    BrowserModule,
    CoreModule,
    DashboardSharedModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

DashboardSharedModule.ts

import { NgModule } from '@angular/core';
import { HeaderComponent } from './header/header.component';
import { MessageSectionComponent } from './message-section/message-section.component';
import { ConversationSectionComponent } from './conversation-section/conversation-section.component';
import { DashboardComponent } from './dashboard.component';
import { DashboardSharedModule } from './shared/shared.module';
import { DashboardRouteModule } from './dashboard.routing';
import { DashboardService } from './dashboard.service';

@NgModule({
  imports: [
    DashboardSharedModule,
    DashboardRouteModule
  ],
  declarations: [
    HeaderComponent,
    MessageSectionComponent,
    ConversationSectionComponent,
    DashboardComponent
  ],
  providers: [DashboardService]
})
export class DashboardModule { }

这两个服务都存在于同一文件夹中。我还在我的shared.module.ts中添加了这两个服务,但是我遇到了以下错误-

错误错误:StaticInjectorError(AppModule)[FileService-> ContactService]:   StaticInjectorError(平台:核心)[FileService-> ContactService]:     NullInjectorError:没有ContactService提供程序!

PS-我从这里删除了一些代码,使其易于理解

2 个答案:

答案 0 :(得分:1)

您需要将DashboardSharedModule导入AppModule并从AppModule中删除ContactService Provider:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app.routing';

import { AppComponent } from './app.component';
import { CoreModule } from './core/core.module';
import { ContactService } from './dashboard/shared/Services/contact.service';
import { ContactService } from './dashboard/shared/Services/contact.service';
//DashboardSharedModule IMPORT STATEMENT

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    AppRoutingModule,
    BrowserModule,
    CoreModule,
    DashboardSharedModule//IMPORT HERE
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

建筑最佳实践

您的体系结构中也有大量的反模式,因为模块不应具有声明/导出和提供程序。

SharedModule应该是小部件功能模块:

  

SharedModule是NgModule的常规名称,带有   您在应用中各处使用的组件,指令和管道。   此模块应完全由声明组成,其中大多数   导出。

     

SharedModule可能会重新导出其他小部件模块,例如   CommonModule,FormsModule和NgModules与您所使用的UI控件   使用最广泛。

     

出于解释原因,SharedModule不应具有提供程序   。其导入或再导出的任何模块也都不应具有   提供者。

     

在已加载的功能模块中导入SharedModule   应用启动时以及以后懒加载的那些。

CoreModule是服务功能模块:

  

CoreModule是NgModule的常规名称,提供了   应用程序启动时加载的单例服务。

     

仅将CoreModule导入到根AppModule中。永远不要导入CoreModule   在任何其他模块中。

     

考虑使CoreModule成为一个纯服务模块,没有   声明。

Feature Module Types

SharedModule and CoreModule

答案 1 :(得分:0)

只需将SharedModule导入app.module.ts数组下的imports中,它就不会引发此错误。