What is store.select in ngrx

2019-01-23 12:51发布

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

4条回答
在下西门庆
2楼-- · 2019-01-23 13:23

In very simple terms select gives you back a slice of data from the application state wrapped into an 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. Lets define the model of our store

    export interface AppStore { clock: Date }

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

  3. Create a store by injecting into the constructor

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

  4. 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');

查看更多
够拽才男人
3楼-- · 2019-01-23 13:26

Wow, this is a big topic. So basically "select" is really a RXJS operator that is used in this case to retrieve the value of a part of the application state object. So say your main app state has a array of users and a array of security functions. "Select" allows you to get a reference to a observable whose value is just that array of users. Before you get into ngrx you really need to study up on Observables and RXJS. I found this article linked off of the main Github project page for ngrx helpful.

https://gist.github.com/btroncone/a6e4347326749f938510

RXJS and redux can be a big topic but I suggest working on your knowledge in small bite size chunks. It took me about 2 months of working with it before I really started to feel comfortable. Even if you don't stay with ngrx, understanding how RXJS works is incredibly useful and is worth the time investment to learn it.

Here is a gist article that also gives a good intro into RXJS. https://gist.github.com/staltz/868e7e9bc2a7b8c1f754

查看更多
该账号已被封号
4楼-- · 2019-01-23 13:28

This.store.select('keyname') will return the data from store object of 'keyname' property has. you can further look for inner object in store using multiple reducer with StoreModule.forFeature("master", masterReducer) in main module with createSelector

export const getMasterState = createFeatureSelector<myModels.MasterState>('master');
export const getMatserBranchList = createSelector(
    getMasterState,
    (state: myModels.MasterState): myModels.Branch[] => state.branchList
);
查看更多
爷的心禁止访问
5楼-- · 2019-01-23 13:31

It returns the state called 'clock'.

Here is an example. In the constructor store.select is called, this time with 'todos'.

https://github.com/btroncone/ngrx-examples/blob/master/todos/src/app/todo-app.ts

export class TodoApp {
    public todosModel$ : Observable<TodoModel>;
    //faking an id for demo purposes
    private id: number = 0;

    constructor(
        private _store : Store<AppState>
    ){
        const todos$ = _store.select<Observable<Todo[]>>('todos');
        const visibilityFilter$ = _store.select('visibilityFilter');

...

In the bootstrap, provideStore is given APP_REDUCERS

import {bootstrap} from '@angular/platform-browser-dynamic';
import {TodoApp} from './todo-app';
import {provideStore} from "@ngrx/store";
import * as APP_REDUCERS from "./reducers/reducers";


export function main() {
  return bootstrap(TodoApp, [
      provideStore(APP_REDUCERS)
  ])
  .catch(err => console.error(err));
}

APP_REDUCERS is all the reducers defined. The todos reducer is defined as follows:

    import {ActionReducer, Action} from "@ngrx/store";
import {Todo} from "../common/interfaces";
import {ADD_TODO, REMOVE_TODO, TOGGLE_TODO} from "../common/actions";

export const todos : ActionReducer<Todo[]> = (state : Todo[] = [], action: Action) => {
  switch(action.type) {
      case ADD_TODO:
          return [
              ...state,
              action.payload
          ];

There are a few ways to do this, and you can compose a list of all your reducers, essentially defining a series of object keys that refer to a reducer object.

Store.select returns an observable that you can subscribe to either in your component or template via '|async'.

查看更多
登录 后发表回答