有人在 lib 裡面用 ngrx 嘛?

使用 ng6

在 app 裡面使用 ngrx 開發了登入/登出功能,並使用 guard(訂閱store) 來控制頁面有登入才能存取。

現在想把這個 登入/登出功能 整理成 lib (ng generate library lib) ,可能實現嗎?
目前覺得似乎不太可能,因為就連 lib 要輸出 service 都要用 forRoot 來實現(?)

請各位大大給點建議,謝謝!

1個讚

ngrx 有 forFeature 可以使用,但前提是你的主 App 也是跑 ngrx 的架構,這樣 library 才會有意義

這樣作法是不是在 “lib module” export StoreModule, “app module” import “lib module” ?
“lib module” 需要實作ModuleWithProviders介面嗎?

目前試過應該可以了,非常感謝~! :grinning:
不用 export StoreModule <—我想太多了
至於 ModuleWithProviders / forRoot() 就跟 service 有關係而已
另外,store 在 lib 和 app之間可以互通,比想像中簡單,大概就和 module 間互通一樣吧!

你這裡可以思考跟路由設定,我們在設定路由的時候,不是有分 forRoot 和 forChild,當我們要做 LazyLoad Module 時,我們會將路由規則設定到 forChild 內
而 ngrx 的 forRoot 與 forFeature 是一樣的道理,都會將 state 合併至同一個 store 內

1個讚