Angular 无法使用角度中的ngrx获取最新状态值
大家好,我是新来的。这些天来,我正在努力学习美国的国家管理。好吧,我试着遵循一些教程,但他们的一些代码已经贬值,所以我有点陷入了一个问题。即使在订阅ts文件后,我也无法获取该文件中的最新状态值Angular 无法使用角度中的ngrx获取最新状态值,angular,ngrx,angular-state-managmement,Angular,Ngrx,Angular State Managmement,大家好,我是新来的。这些天来,我正在努力学习美国的国家管理。好吧,我试着遵循一些教程,但他们的一些代码已经贬值,所以我有点陷入了一个问题。即使在订阅ts文件后,我也无法获取该文件中的最新状态值 //Action File import { Action } from '@ngrx/store'; export enum CategoriesActionTypes { LoadCategoriess = '[Categories] Load Categoriess', LoadCateg
//Action File
import { Action } from '@ngrx/store';
export enum CategoriesActionTypes {
LoadCategoriess = '[Categories] Load Categoriess',
LoadCategoriessSuccess = '[Categories] Load Categoriess Success',
LoadCategoriessFailure = '[Categories] Load Categoriess Failure',
}
export class LoadCategoriess implements Action {
readonly type = CategoriesActionTypes.LoadCategoriess;
}
export class LoadCategoriessSuccess implements Action {
readonly type = CategoriesActionTypes.LoadCategoriessSuccess;
constructor(public payload: { data: any }) { }
}
export class LoadCategoriessFailure implements Action {
readonly type = CategoriesActionTypes.LoadCategoriessFailure;
constructor(public payload: { error: any }) { }
}
export type CategoriesActions = LoadCategoriess | LoadCategoriessSuccess | LoadCategoriessFailure;
//减速器锉
import { CategoriesActions, CategoriesActionTypes } from '../Actions/categories.actions';
import { Action } from '@ngrx/store';
export const categoriesFeatureKey = 'categoriesState';
export interface State {
categories :any,
error: any
}
export const initialState: State = {
categories:[{name:'name'}],
error :null,
};
export function reducer(state = initialState, action: CategoriesActions): State {
switch (action.type) {
case CategoriesActionTypes.LoadCategoriess:
return{
...state
}
case CategoriesActionTypes.LoadCategoriessSuccess:
// debugger
console.log(action.payload.data);
return {
...state,
categories: action.payload.data,
error: ''
}
case CategoriesActionTypes.LoadCategoriessFailure:
return {
...state,
categories: [],
error: action.payload.error
}
default:
return state;
}
}
//效果文件
import { FrontserveiceService } from './../front/frontserveice.service';
import { Injectable } from '@angular/core';
import { Actions, Effect, createEffect, ofType } from '@ngrx/effects';
import { Observable, of } from 'rxjs';
import { Action } from '@ngrx/store';
import * as CategoriesActions from './../Actions/categories.actions';
import { mergeMap, map, catchError } from 'rxjs/operators';
@Injectable()
export class CategoriesEffects {
constructor(private actions$: Actions,private service:FrontserveiceService) {}
// @Effect()
loadCategories$: Observable<any> = createEffect(() =>
this.actions$.pipe(
ofType(CategoriesActions.CategoriesActionTypes.LoadCategoriess),
mergeMap(
action => this.service.CategoryList({ image: true }).pipe(
map(users => {
return (new CategoriesActions.LoadCategoriessSuccess({ data: users }));
}),
catchError(err => of(new CategoriesActions.LoadCategoriessFailure({ error: err })))
)
)
));
}
//选择器
import { State } from './../Reducerss/categories.reducer';
import { createFeatureSelector, createSelector } from '@ngrx/store';
const getCategoryFeatureState = createFeatureSelector<State>('categoriesState');
export const getUsers = createSelector(
getCategoryFeatureState,
state => state.categories
)
export const getError = createSelector(
getCategoryFeatureState,
state => state.error
)
从“/../reducers/categories.reducer”导入{State};
从'@ngrx/store'导入{createFeatureSelector,createSelector};
常量getCategoryFeatureState=createFeatureSelector('categoriesState');
export const getUsers=createSelector(
getCategoryFeatureState,
state=>state.categories
)
export const getError=createSelector(
getCategoryFeatureState,
state=>state.error
)
我现在拿到了。我试图访问一部分不允许访问的状态,这就是我无法访问订阅中状态的原因。您可以共享您的订阅吗?另外,状态截图(redux开发工具)将有助于理解问题OK让我编辑我的问题,您可以共享您的选择代码吗?上传的选择代码
import { State } from './../Reducerss/categories.reducer';
import { createFeatureSelector, createSelector } from '@ngrx/store';
const getCategoryFeatureState = createFeatureSelector<State>('categoriesState');
export const getUsers = createSelector(
getCategoryFeatureState,
state => state.categories
)
export const getError = createSelector(
getCategoryFeatureState,
state => state.error
)