Vel*_*dan 2 typescript angular
我尝试创建一些核心模块,就像在教程中除了一个细节,我想只提供服务.所以我的CoreModule应该像 - 单身服务提供者,因为我不想在AppModule中提供大量服务,就像应用版本<= RC4一样.我试着做那些东西,但它不起作用.我做错了吗?谢谢你的帮助.
import {
ModuleWithProviders, NgModule,
Optional, SkipSelf } from '@angular/core';
import { CommonModule } from '@angular/common';
import { CommunicatePatientListService } from './services/communicate_patients_list.service';
import { HTTPPatientsListService } from './services/http_patients_list.service';
import { SharedService } from './services/shared_service';
@NgModule({
imports: [ CommonModule ],
providers: [
CommunicatePatientListService,
HTTPPatientsListService,
SharedService
],
exports: []
})
export class CoreModule {}
Run Code Online (Sandbox Code Playgroud)
更新2.我尝试了不同的方法来完成提供给我的事情,我发现了一个有趣的时刻.
当我通过标准导入导入COREModule单例服务时,它不起作用,但是当我通过System.js别名导入它时,它现在正在工作.我真的很想知道它是如何工作的.这是代码
CoreModule:
import {
ModuleWithProviders, NgModule,
Optional, SkipSelf } from '@angular/core';
import { CommonModule } from '@angular/common';
import { HeaderModule } from './modules/header/header.module';
import { FooterComponent } from './modules/footer/footer.component';
//THAT DOESNT WORK
// import { CommunicatePatientListService } from './services/communicate_patients_list.service';
// import { HTTPPatientsListService } from './services/http_patients_list.service';
// import { SharedService } from './services/shared_service';
// import { SchedulerSharedService } from './services/scheduler_shared.service';
// import { FormChangeService } from './services/on_form_change.service';
//IT IS WORKING NOW
import { CommunicatePatientListService } from '%sharedServices%/communicate_patients_list.service';
import { HTTPPatientsListService } from 'httpPatientsListService';
import { SharedService } from 'sharedService';
import { SchedulerSharedService } from 'schedulerSharedService';
import { FormChangeService } from 'formChangeService';
@NgModule({
imports: [
CommonModule,
HeaderModule,
],
declarations: [
FooterComponent
],
exports: [
FooterComponent,
HeaderModule
]
})
export class CoreModule {
constructor (@Optional() @SkipSelf() parentModule: CoreModule) {
if (parentModule) {
throw new Error(
'CoreModule is already loaded. Import it in the AppModule only');
}
}
static forRoot(): ModuleWithProviders {
return {
ngModule : CoreModule,
providers: [
CommunicatePatientListService,
HTTPPatientsListService,
SharedService,
FormChangeService,
SchedulerSharedService
]
};
}
}
Run Code Online (Sandbox Code Playgroud)
患者列表组件
import { Component, Input, OnInit, ViewChild } from '@angular/core';
import { CommunicatePatientListService } from '%sharedServices%/communicate_patients_list.service';
import { HTTPPatientsListService } from 'httpPatientsListService';
import { SharedService } from 'sharedService';
import { SchedulerSharedService } from 'schedulerSharedService';
import { FormChangeService } from 'formChangeService';
import { Config } from 'appConfig';
/* ------- !Config ---------*/
const MODULE_NAME: string = 'patients_list';
const MODULE_PATH: string = `${Config.getProdFolderName()}/modules/patients/${MODULE_NAME}`;
@Component({
templateUrl: `${MODULE_PATH}/${MODULE_NAME}.component.html`,
styleUrls: [`${MODULE_PATH}/${MODULE_NAME}.component.css`,]
})
export class PatientsListComponent implements OnInit {
constructor(
private patientsListService:HTTPPatientsListService,
private patsListServCom:CommunicatePatientListService,
private schedulerSharedService:SchedulerSharedService,
private sharedService:SharedService
) {
}
Run Code Online (Sandbox Code Playgroud)
var*_*ius 15
最好的方法是使用提供程序创建模块.请记住,如果您的服务位于共享模块中,您可能会获得多个实例.最好的办法是配置模块Module.forRoot.
按照惯例,forRoot静态方法同时提供和配置服务.它需要一个服务配置对象并返回一个ModuleWithProviders.
这是关于它的完整Doc.
仅在根应用程序模块AppModule中调用.在任何其他模块中调用它,特别是在延迟加载的模块中,与意图相反,并且可能产生运行时错误.
记得导入结果; 不要将它添加到任何其他@NgModule列表.
@NgModule({
imports: [CommonModule],
declarations: [SomeComponent],
exports: [SomeComponent]
})
export class CoreModule {
static forRoot(): ModuleWithProviders {
return {
ngModule: CoreModule,
providers: [SomeService]
};
}
}
Run Code Online (Sandbox Code Playgroud)
然后导入模块看起来像:
@NgModule({
imports: [
/** other modules import **/
CoreModule.forRoot(), // you can also pass some config here if needed
routing
],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)
如果要防止重新导入CoreModule
只有根AppModule才能导入CoreModule.如果延迟加载的模块导入它,则会发生错误.
@NgModule({
imports: [ CommonModule ],
declarations: [ SomeComponent ],
exports: [ SomeComponent ],
})
export class CoreModule {
constructor (@Optional() @SkipSelf() parentModule: CoreModule) {
if (parentModule) {
throw new Error(
'CoreModule is already loaded. Import it in the AppModule only');
}
}
static forRoot(config: UserServiceConfig): ModuleWithProviders {
return {
ngModule: CoreModule,
providers: [SomeService]
};
}
}
Run Code Online (Sandbox Code Playgroud)