Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 从ngrx/存储获取数据_Angular_Ngrx - Fatal编程技术网

Angular 从ngrx/存储获取数据

Angular 从ngrx/存储获取数据,angular,ngrx,Angular,Ngrx,对于简单的类型,所有这些都可以很好地工作,但我无法处理对象 这是我的减速机: import { Action } from '@ngrx/store' import { RssFeed } from "../rss/rss"; export interface AppState { demoData: number; feeds: RssFeed[]; } const initialState: AppState = { demoData: 0, feeds: [] };

对于简单的类型,所有这些都可以很好地工作,但我无法处理对象

这是我的减速机:

import { Action } from '@ngrx/store'
import { RssFeed } from "../rss/rss";

export interface AppState {
  demoData: number;
  feeds: RssFeed[];
}

const initialState: AppState = {
  demoData: 0,
  feeds: []
};

export const INCREMENT = 'INCREMENT';
export const DECREMENT = 'DECREMENT';

export function feedsReducer(state: AppState = initialState, action: Action): AppState {

  switch(action.type) {
    case INCREMENT: {

      return {
        demoData: state.demoData + 1,
        feeds: []
      }
    }
    case DECREMENT: {

        return {
          demoData: state.demoData - 1,
          feeds: []
        }
      }
    default:
      return state;
  }
}
app.module:

import { StoreModule } from '@ngrx/store';
import { feedsReducer } from './actions/rss';

imports: [
    StoreModule.provideStore({ feeds: feedsReducer })
  ],
组成部分:

import {Store } from '@ngrx/store';
import { AppState, INCREMENT, DECREMENT } from '../actions/rss'

export class RssComponent {

  state: Observable<AppState>;

  constructor(private store: Store<AppState>) {
    this.state = store.select('feeds');

addDemoData() {
    console.log(this.state);
    this.store.dispatch({type: INCREMENT})
  }
而不是我的州。为什么?

我认为这里的错误是:

store.select('feeds');

但是我不知道要传递给store.select()。

我这样修复它,但我不确定这是否是一个好方法:

store.select('feedsReducer').subscribe((data: AppState) => this.state = data );

下面是一个能够推断类型的简单解决方案(比使用字符串更好):


您可以在模板中使用异步管道(请记住,提要是可观察的,而不是数组):

--feeds.component.ts
this.feeds=store.select(state=>state.feeds)
--feed.component.html

参考:

store.select('feedsReducer').subscribe((data: AppState) => this.state = data );
store
  .select(state => state.feeds)
  .do(feeds => this.feeds = feeds)
  .subscribe();
-- feeds.component.ts

this.feeds = store.select(state => state.feeds)

-- feed.component.html

<li *ngFor="let feed of feeds | async">