gpt4 book ai didi

angular - 在 Angular 2 中提供核心单例服务模块

转载 作者:太空狗 更新时间:2023-10-29 19:36:30 25 4
gpt4 key购买 nike

除了一个细节,我尝试创建一些核心模块,就像在教程中一样,我只想提供服务。所以我的 CoreModule 应该是 - 单例服务提供者,因为我不想像应用程序版本 <= RC4 那样在 AppModule 中提供大量服务。我试图做那些事情,但它不起作用。我做错了什么?感谢您的帮助。

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 {}

更新 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
]
};
}


}

患者列表组件

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
) {
}

最佳答案

最好的方法是使用提供程序创建模块。请记住,如果您的服务在共享模块中,您可能会获得它的多个实例。最好的办法是使用 Module.forRoot 配置模块。

By convention, the forRoot static method both provides and configures services at the same time. It takes a service configuration object and returns a ModuleWithProviders.

这里是完整的Doc关于它。

Call forRoot only in the root application module, AppModule. Calling it in any other module, particularly in a lazy loaded module, is contrary to the intent and is likely to produce a runtime error.

Remember to import the result; don't add it to any other @NgModule list.

@NgModule({
imports: [CommonModule],
declarations: [SomeComponent],
exports: [SomeComponent]
})
export class CoreModule {
static forRoot(): ModuleWithProviders {
return {
ngModule: CoreModule,
providers: [SomeService]
};
}
}

然后导入模块看起来像:

@NgModule({
imports: [
/** other modules import **/
CoreModule.forRoot(), // you can also pass some config here if needed
routing
],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }

如果你想阻止重新导入 CoreModule

Only the root AppModule should import the CoreModule. Bad things happen if a lazy loaded module imports it.

@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]
};
}
}

关于angular - 在 Angular 2 中提供核心单例服务模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39890722/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com