Angular2 - Share data between components using services

前端 未结 3 1814
灰色年华
灰色年华 2020-11-29 06:20

I have an object that I want to share between my components into an Angular2 app.

Here is the source of the first component:

/* app.component.ts */

         


        
3条回答
  •  粉色の甜心
    2020-11-29 07:13

    For the latest version of angular, if you want to share the service, you can't add it to the bootstrap function. Just add it to the NgModule providers list as you would do with a normal service, its default behaviour will be singleton.

    bootstrap(AppComponent);

    @NgModule({
        declarations: [
            ....
        ],
        imports: [
           ....     
        ],
        providers: [
            ConfigService,
    ....
    

提交回复
热议问题