简体   繁体   English

Angular 2 RC.5单例全局共享服务

[英]Angular 2 RC.5 singleton global shared service

I am trying to create global singleton available services . 我正在尝试创建全球单例可用服务 I have read this solution - https://angular.io/docs/ts/latest/guide/ngmodule.html#!#shared-module , but it doesn't works for me. 我已经阅读了此解决方案-https://angular.io/docs/ts/latest/guide/ngmodule.html#!#shared - module ,但不适用于我。

I created shared module (this is my shorter example): 我创建了共享模块 (这是我的简短示例):

NgModule({
    imports: [
        CommonModule,
        ReactiveFormsModule
    ],
    declarations: [ ShopCartComponent ],
    exports: [
        CommonModule,
        ReactiveFormsModule,
        ShopCartComponent,
    ]
})
export class SharedModule {
    static forRoot() : ModuleWithProviders {
        return {
            ngModule: SharedModule,
            providers: [
                ProductsService,
                ProductsActions
            ]
        };
    }
}

And in my root application module: 在我的应用程序模块中:

NgModule({
    imports: [
        BrowserModule,
        SharedModule.forRoot(),
        RouterModule.forRoot(routes)
    ],
    declarations: [ AppComponent ],
    providers: [
        provideStore({
           ... some stores ...
        })
    ],
    bootstrap: [ AppComponent ]
})
export class AppModule { }

But when I am trying to inject ProductsService | 但是,当我尝试注入ProductsService时| ProductsActions into children module, angular shows followed error message: ProductsActions进入模块,角度显示跟随错误消息:

ORIGINAL EXCEPTION: No provider for ProductsService! 原来的例外:没有ProductsService的提供者!

It is my children module: 这是我的孩子模块:

@NgModule({
    imports: [
        SharedModule,
        RouterModule.forChild([
            { path: '', component: ProductsComponent }
        ])
    ],
    declarations: [ ProductsComponent ]
})
export default class ProductsModule { }

I already imported SharedModule. 我已经导入了SharedModule。 Shoud I do something more? 我应该做更多的事情吗?

---------------- UPDATE ------------------- ----------------更新-------------------

I already solved my problem. 我已经解决了我的问题。 I had to add ProductsModule into imports of root module. 我必须将ProductsModule添加到根模块的导入中。 Like this: 像这样:

@NgModule({
    imports: [
        BrowserModule,
        ProductsModule,
        RouterModule.forRoot(routes),
        SharedModule.forRoot()
    ],
    declarations: [ AppComponent ],
    bootstrap: [ AppComponent ]
})
export class AppModule { }

The reason (I think) is that I wanted to load another module immadiately in root route table, like this: 原因(我认为)是我想立即在根路由表中加载另一个模块,如下所示:

{ path: '', loadChildren: 'app/components/products/products.module' },

Thanks guys for all help! 谢谢大家的帮助!

I would set your services into the providers attribute of your shared module. 我会将您的服务设置为共享模块的providers属性。

@NgModule({
  imports: [
    CommonModule,
    ReactiveFormsModule
  ],
  declarations: [ ShopCartComponent ],
  exports: [
    CommonModule,
    ReactiveFormsModule,
    ShopCartComponent,
  ],
  providers: [ // <-----
    ProductsService,
    ProductsActions
  ]
})
export class SharedModule {
  (...)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM