ngrx/store 将商店注入服务

ngrx/store Inject a store into a service

我正在使用 ngrx/store v4.0.2,Angular 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,而商店对于同一模块中的组件工作得很好。

如何从服务内部访问组件可用的同一个商店?

这是原话:http://plnkr.co/edit/1Lxq82

编辑 - 问题已修复
问题出在 getIsLoggedIn 上。它是:

export const getIsLoggedIn = (state: AuthDataState) => state.IsLoggedIn;

应该是:

export const getIsLoggedIn = createFeatureSelector<AuthDataState>('feature1');

我在您的 plnkr 中发现了一些错误

1.) 您需要在跟踪器服务前加上@Injectable()

前缀

2.) 您在跟踪器服务上的 wasInjected 询问 == null 如果它不为空,它将 return 为假,如果您是问问题 "Was Injected"。所以我倒转了那张支票。

在此处更新了 plnkr http://plnkr.co/edit/KVLpHS