Angular ngrx/存储将存储注入服务
我使用的是ngrx/store v4.0.2和Angular 4.3.3 我尝试从服务中访问存储:Angular ngrx/存储将存储注入服务,angular,dependency-injection,ngrx,ngrx-store,Angular,Dependency Injection,Ngrx,Ngrx Store,我使用的是ngrx/store v4.0.2和Angular 4.3.3 我尝试从服务中访问存储: /*******模块*************/ 从'@ngrx/Store'导入{StoreModule,Store}; @NGD模块({ 进口:[ 公共模块, StoreModule.forFeature('feature1',减速器), //... ], 声明:[/*…*/], 出口:[], 供应商:[ AuthDataService ] 导出类MyModule{} /*********Au
/*******模块*************/
从'@ngrx/Store'导入{StoreModule,Store};
@NGD模块({
进口:[
公共模块,
StoreModule.forFeature('feature1',减速器),
//...
],
声明:[/*…*/],
出口:[],
供应商:[
AuthDataService
]
导出类MyModule{}
/*********AuthDataService*************/
从'@ngrx/Store'导入{Store};
从“../reducers/auth”导入{AuthDataState};
导出类AuthDataService{
构造函数(私有状态:存储)
编辑-问题已修复
问题在于getIsLoggedIn
export const getIsLoggedIn = (state: AuthDataState) => state.IsLoggedIn;
应该是:
export const getIsLoggedIn = createFeatureSelector<AuthDataState>('feature1');
export const getIsLoggedIn=createFeatureSelector('feature1');
我在您的plnkr中发现了一些错误
1.)您需要在跟踪器服务前面加上@Injectable()
2.)您在tracker服务上的被注入
是在询问==null
,如果它不是null,它将返回false,如果您问的是“被注入”的问题,那么这就没有意义了。因此,我将该检查颠倒过来
这里更新了plnkr
不,不是这样……我添加了plnkr链接,祝你玩得开心:)更新了我的答案,发现了你的plnkr中的问题。此外,我分叉了你的plnkr(上面的链接),并去掉了一堆东西来查找错误。(存储路由器的东西,还有什么不可以)。应该与最终答案没有什么关系。谢谢…修复了plnkr,我还发现了选择器的问题-我直接访问了商店,我应该使用createFeatureSelector
(即,createSelector
…在我的原始版本中,我将其简化为SO,该版本位于向StoreModule.forFeature
注册存储的子模块中)