/app
- app.component.ts
- app.component.html (hide/show: menu bar)
- app.global.service.ts (Public varible LoginSuccess:boolean)
- main.ts
/student
- student.ts
- student.service.ts
- student.component.ts
- student.component.html
/security
- login.component.ts (LoginSuccess = true)
- login.component.html
在我的 Angular2 应用程序中,我有一个简单的需求,我想根据登录成功显示隐藏菜单栏。为此,我创建了一个服务,它只有一个 LoginSuccess 布尔变量,我将在登录组件上设置它并将在 app.component.html 上用于[hidden]=LoginSuccess
导航标记。
我面临的问题是,即使在app.global.service.ts
通过constructor
值注入app.component.ts & login.component.ts
并没有持续存在并且每个构造函数都创建app.global.service.ts
.
问题:如何通过服务在应用程序中保持单一价值。在 Angular2 文档的某个地方,我确实读过 Injectable 服务是单例的。