Angular2(六)--单一实例(singleton)示例

来源:互联网 发布:手机游戏录像软件 编辑:程序博客网 时间:2024/06/06 12:31

to have only one singleton in the entire application, disregarding if our modules are being loaded at bootstrap or lazy loaded

单一实例:

import { NgModule, ModuleWithProviders } from '@angular/core';/* ...other imports... */@NgModule({  imports: [CommonModule],  declarations: [    CreditCardMaskPipe,    CreditCardComponent  ],  exports: [CreditCardComponent]})export class CreditCardModule {  static forRoot(): ModuleWithProviders {    return {      ngModule: CreditCardModule,      providers: [CreditCardService]    }  }}

always use the forRoot syntax when exporting services from feature modules

使用方法:

@NgModule({  imports: [    BrowserModule,    CreditCardModule.forRoot()  ],  declarations: [AppComponent],  bootstrap: [AppComponent]})export class AppModule { }
0 0
原创粉丝点击