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

查看:477
本文介绍了什么是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

导出界面AppStore {
clock:Date
}

export interface AppStore { clock: Date }

从'@ngrx / store'将商店导入您的组件

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

通过注入创建商店进入构造函数

Create a store by injecting into the constructor

构造函数(private _store:Store< AppStore>){}

选择返回一个Observable。

Select returns an Observable.

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

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

公共时钟:Observable< Date> ;;

现在你可以做点什么如下所示: -

Now you can do something like follows:-

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

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

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