Angular 2 Shared Data Service is not working

后端 未结 2 2027
庸人自扰
庸人自扰 2021-01-25 15:00

I have built a shared data service that\'s designed to hold the users login details which can then be used to display the username on the header, but I cant get it to work.

2条回答
  •  面向向阳花
    2021-01-25 15:34

    Add it in @NgModule.providers array of your AppModule:

    if you add it in @Component.providers array then you are limiting the scope of SharedDataService instance to that component and its children.

    in other words each component has its own injector which means that headerComponentwill make its own instance of SharedDataServiceand loginComponent will make its own instance.

提交回复
热议问题