首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ngrx/商店将商店注入服务

ngrx/商店将商店注入服务
EN

Stack Overflow用户
提问于 2017-08-21 20:46:11
回答 1查看 6.1K关注 0票数 1

我使用的是ngrx/store v4.0.2,带有角4.3.3。

我想要做的是从服务中访问商店:

代码语言:javascript
复制
/********* 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。它是:

代码语言:javascript
复制
export const getIsLoggedIn = (state: AuthDataState) => state.IsLoggedIn;

它应该是:

代码语言:javascript
复制
export const getIsLoggedIn = createFeatureSelector<AuthDataState>('feature1');
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-21 22:50:15

我在你的plnkr里发现了一些错误

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

2.)您在跟踪器服务上的wasInjected询问的是== null,如果它不是null,它将返回false,如果您问的问题是“被注入的”,这是没有意义的。所以我倒转了那张支票。

更新了这里的plnkr http://plnkr.co/edit/KVLpHS

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45805265

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档