/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
nav標記。我對着Angular2全球服務提供商
問題是,即使注入app.global.service.ts
通app.component.ts & login.component.ts
價值constructor
不是持久的,並且每個構造函數創建的app.global.service.ts
新對象後。
問題:如何才能實現跨應用程序通過服務保持單個值。在Angular2文檔中的某處,我讀了Injectable服務是單例。
什麼樣,如果我們想在引導的時候提供一些組件,如我們在Globalservice中做過? –
只是說:如果注入singleton(全局)服務的組件位於單獨的文件中,那麼您仍然必須在該文件中使用import語句來加載服務:從「./」導入{AuthService}。 services/auth.service「;' – Samuel
但是,如果我不把它添加到提供者:[]我得到一個錯誤; 例外:沒有GlobalService的提供者! –