简体   繁体   English

ngRx Store - Reducer 返回未定义 - store.select() 未定义

[英]ngRx Store - Reducer returns undefined - store.select() is undefined

it's my first time working with ngRx Store and I implemented my first effect..这是我第一次使用 ngRx Store,我实现了我的第一个效果..

What is working fine:什么工作正常:

  • communication with backend与后端通信
  • actions are being executed in backend (delete, add, get)正在后端执行操作(删除、添加、获取)
  • payload is getting to the reducer from effects (logged it to be sure)有效载荷从效果到达减速器(记录下来以确保)

So my problem is that I can not get Data from my reducer.所以我的问题是我无法从减速器中获取数据。 If I select the data from the store in my component, I get undefined data in the component.如果我 select 将数据从存储在我的组件中,我会在组件中获得未定义的数据。 I tapped the data in the effects and the data is definietly passed.我点击了效果中的数据,数据被明确传递了。 Also logged the data in the reducer - it is there.还将数据记录在减速器中 - 它就在那里。 But when it is returned, it returns undefined.但是当它返回时,它返回未定义。 ReDux Devtools logs everything fine. ReDux Devtools 记录一切正常。 And last but not least.. the state is not updated, even with fixed values, like a boolean with true/false for loading.最后但并非最不重要的一点.. state 没有更新,即使是固定值,如 boolean 与 true/false 用于加载。

Le Code:乐代码:

// Where I get undefined // 我得到未定义的地方

export class ProfileComponent implements OnInit {

  user: any;
  items$: Observable<Product[]>;
  loading$: Observable<boolean>;
  error$: Observable<Error>;
  newItem: Product = {id: 0, name:""};

  constructor(private keycloakService: KeycloakService, private store: Store<ProductState>, private productService: ProductService) { }

  ngOnInit(): void {
    this.productService.setToken(this.keycloakService.getToken());

    this.user = this.keycloakService.getUsername();

    this.items$ = this.store.select(store => store.list);
    this.loading$ = this.store.select( store => store.loading);
    this.error$ = this.store.select( store => store.error);

    this.store.dispatch(new LoadProduct());

    this.items$.subscribe(val => console.log(val));
    this.loading$.subscribe(val => console.log(val));
    this.error$.subscribe(val => console.log(val));
  }

// Reducer // 减速器

export type Action = Products.All;

const initialState: ProductState = {
  list: [],
  loading: false,
  error: undefined
}

const newState = (state, newData) => {
  return Object.assign({}, state, newData);
}

export function reducer(state: ProductState = initialState, action: Action) : ProductState{
  console.log(action.type, state);

  switch (action.type) {
    case Products.LOAD_PRODUCT: return { ...state, loading: true};
    case Products.LOAD_PRODUCT_SUCCESS: return newState(state, {list: action.payload, loading: false, error: undefined});
    case Products.LOAD_PRODUCT_FAILURE: return { ...state, error: action.payload, loading: false};
    case Products.ADD_PRODUCT: return { ...state, loading: true};
    case Products.ADD_PRODUCT_SUCCESS: return { ...state, list: action.payload, loading: false};
    case Products.ADD_PRODUCT_FAILURE: return { ...state, error: action.payload, loading: false};
    case Products.REMOVE_PRODUCT: return { ...state, loading: true};
    case Products.REMOVE_PRODUCT_SUCCESS: return { ...state, list: state.list.filter(item => item.id !== action.payload), loading: false};
    case Products.REMOVE_PRODUCT_FAILURE: return { ...state, error: action.payload, loading: false};
    default: return state;
  }

I appreciate your help very much:)我非常感谢您的帮助:)

You should use selector functions to get the data from the store: https://ngrx.io/guide/store/selectors您应该使用选择器函数从商店获取数据: https://ngrx.io/guide/store/selectors

For a quick unblock, I bet .list, .loading, .error are undefined on state .为了快速解除阻塞,我敢打赌 state 上未定义.list, .loading, .error state

Try this to debug:试试这个来调试:

// see how the whole store looks like
this.store.subscribe(state => console.log({ state }));

And for you, I see it is related to products , so I bet it should be something like this:对你来说,我认为它与products相关,所以我敢打赌它应该是这样的:

this.items$ = this.store.select(store => store.products.list);
this.loading$ = this.store.select( store => store.products.loading);
this.error$ = this.store.select( store => store.products.error);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM