ngrx 中的 store.select 是什么 [英] What is store.select in ngrx

查看:25
本文介绍了ngrx 中的 store.select 是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Redux 的新手,从 ngrx 开始.我无法理解这行代码的含义 store.select:

I'm new to Redux and started with ngrx. I'm unable to understand the meaning of this line of code store.select:

 clock: Observable<Date>;
 this.clock = store.select('clock');

推荐答案

用非常简单的术语来说,select 会返回来自应用程序状态的一部分数据,并将其封装到 Observable 中.

In very simple terms select gives you back a slice of data from the application state wrapped into an Observable.

它的意思是,select 操作符获取你需要的数据块,然后将它转换成一个 Observable 对象.所以,你得到的是一个封装了所需数据的 Observable.要使用您需要订阅的数据.

What it means is, select operator gets the chunk of data you need and then it converts it into an Observable object. So, what you get back is an Observable that wraps the required data. To consume the data you need to subscribe to it.

让我们看一个非常基本的例子.

Lets see a very basic example.

  1. 让我们定义我们商店的模型

  1. Lets define the model of our store

export interface AppStore {
   clock: Date
}

  • 将 Store 从@ngrx/store"导入您的组件

  • Import the Store into your component from '@ngrx/store'

    通过注入构造函数来创建存储

    Create a store by injecting into the constructor

    constructor(private _store: Store<AppStore>){}
    

  • Select 返回一个 Observable.

  • Select returns an Observable.

    因此,在您的组件中声明时钟变量如下:-

    So, declare the clock variable in your component as follows:-

    public clock: Observable<Date>;
    

    现在您可以执行以下操作:-

    Now you can do something like follows:-

    this.clock = this._store.select('clock');
    

  • 这篇关于ngrx 中的 store.select 是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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