简体   繁体   English

无需订阅即可获取当前的 ngrx 状态

[英]Get current ngrx state without subscribe

I'd like to use Ngrx for OAuth2 services and I need to get current store state without using subscribe.我想将 Ngrx 用于 OAuth2 服务,我需要在不使用订阅的情况下获取当前商店状态。

This is my OAuth function for getting token from localStorage:这是我用于从 localStorage 获取令牌的 OAuth 函数:

private oAuth2() {
    const token = JSON.parse(localStorage.getItem('oAuthToken'));
    if (token && token.access_token) {
        const headers = new Headers({ 'Authorization': 'Bearer ' + token.access_token });
        return new RequestOptions({ headers: headers });
    }
}

Is it possible to use Redux store instead of localStorage in this case?在这种情况下,是否可以使用 Redux 存储而不是 localStorage?

I really don't see why you want to get the state without subscribing to it as this is the core concept of Ngrx and you can't get a value from a RxJS Subject unless you subscribe to it:我真的不明白为什么要在不订阅的情况下获取状态,因为这是 Ngrx 的核心概念,除非订阅 RxJS 主题,否则无法从 RxJS 主题中获取值:

The two pillars of @ngrx/store, the store and dispatcher, both extend RxJS Subjects. @ngrx/store 的两大支柱,store 和 dispatcher,都扩展了 RxJS Subjects。 Subjects are both Observables and Observers, meaning you can subscribe to a Subject, but can also subscribe a Subject to a source. Subjects 既是 Observables 又是 Observers,这意味着你可以订阅一个 Subject,但也可以订阅一个 Subject 到一个源。 At a high-level Subjects can be thought of as messengers, or proxies.在高层次上,Subjects 可以被认为是信使或代理。

I hope this explains to you that the answer is NO.我希望这向您解释了答案是否定的。 You can't get current Ngrx state without subscribing to it!不订阅就无法获得当前的 Ngrx 状态!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM