获取当前的ngrx状态而无需订阅 [英] Get current ngrx state without subscribe

查看:85
本文介绍了获取当前的ngrx状态而无需订阅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将Ngrx用于OAuth2服务,我需要在不使用订阅的情况下获取当前的存储状态.

I'd like to use Ngrx for OAuth2 services and I need to get current store state without using subscribe.

这是我的OAuth函数,用于从localStorage获取令牌:

This is my OAuth function for getting token from localStorage:

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 });
    }
}

在这种情况下是否可以使用Redux存储代替localStorage?

Is it possible to use Redux store instead of localStorage in this case?

推荐答案

我真的不明白为什么要在不订阅状态的情况下获得状态,因为这是Ngrx的核心概念,您无法获得价值来自RxJS主题,除非您订阅它:

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/store的两个支柱,即store和dispatcher,都延伸 RxJS主题.主题既是可观察者又是观察者,这意味着 您可以订阅主题,但也可以订阅主题 来源.在较高的层次上,可以将主题视为信使,或者 代理.

The two pillars of @ngrx/store, the store and dispatcher, both extend RxJS Subjects. Subjects are both Observables and Observers, meaning you can subscribe to a Subject, but can also subscribe a Subject to a source. At a high-level Subjects can be thought of as messengers, or proxies.

我希望这可以向您解释答案是否定的.如果不订阅,就无法获得当前的Ngrx状态!

I hope this explains to you that the answer is NO. You can't get current Ngrx state without subscribing to it!

这篇关于获取当前的ngrx状态而无需订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆