我使用的是ngrx/store v4.0.2,带有角4.3.3。
我想要做的是从服务中访问商店:
/********* Module *************/
import { StoreModule, Store } from '@ngrx/store';
@NgModule({
imports: [
CommonModule,
StoreModule.forFeature('feature1', reducers),
//...
],
declarations: [ /*...*/ ],
exports: [],
providers: [
AuthDataService
]
export class MyModule { }
/********* AuthDataService *************/
import { Store } from '@ngrx/store';
import { AuthDataState } from '../reducers/auth';
export class AuthDataService {
constructor(private state: Store<AuthDataState>){
this.state.select(getIsLoggedIn).subscribe(in => {
console.log("User 'loggedIn' is now set to:" + in);
});
}
}但是,我总是得到undefined,而存储对于同一个模块中的组件来说却很好。
如何从服务中访问组件可以访问的同一存储库?
下面是plunk:http://plnkr.co/edit/1Lxq82
编辑-发行固定
问题在于getIsLoggedIn。它是:
export const getIsLoggedIn = (state: AuthDataState) => state.IsLoggedIn;它应该是:
export const getIsLoggedIn = createFeatureSelector<AuthDataState>('feature1');发布于 2017-08-21 22:50:15
我在你的plnkr里发现了一些错误
1.)您需要在跟踪器服务前加上@Injectable()。
2.)您在跟踪器服务上的wasInjected询问的是== null,如果它不是null,它将返回false,如果您问的问题是“被注入的”,这是没有意义的。所以我倒转了那张支票。
更新了这里的plnkr http://plnkr.co/edit/KVLpHS
https://stackoverflow.com/questions/45805265
复制相似问题