Angular2 global service provider

后端 未结 5 1558
轮回少年
轮回少年 2020-12-14 20:49
/app
        - app.component.ts 
        - app.component.html (hide/show: menu bar)
        - app.global.service.ts (Public varible LoginSuccess:boolean)
        - m         


        
5条回答
  •  醉酒成梦
    2020-12-14 21:04

    As Saxsa, the key point is to define your service provider within the application injector and not at each component level. Be careful not to define the service provider twice... Otherwise you will still have separate service instances.

    This way you will be able to share the same instance of the service.

    This behavior occurs because of hierarchical injectors of Angular2. For more details, you could have a look at this question:

    • What's the best way to inject one service into another in angular 2 (Beta)?

提交回复
热议问题