NGX选择器不唤起数据
我有这个
reducer.ts
export const userFeatureKey = 'user';
export interface UserState {
selectedUser: User | null,
users: User[],
isLoading?: boolean;
error?: any;
}
export const initialState: UserState = {
selectedUser: null,
users: [],
isLoading: false,
error: null
};
export const reducer = createReducer(
initialState,
on(loadUsers, (state) => ({...state, isLoading: true})),
on(loadUsersSuccess, (state, {data: users}) => ({...state, users, isLoading: false})),
on(loadUsersFailure, (state, error) => ({...state, error: error, isLoading: false})),
on(saveUserFailureAction, (state, error) => ({...state, isLoading: false, error: error})),
on(saveUserSuccessAction, (state, {data: user}) => ({
...state,
isLoading: false,
selectedUser: user,
users: [...state.users, user]
})
),
);
index.ts 注册所有还原器
import {ActionReducerMap, MetaReducer} from '@ngrx/store';
import {environment} from '../../environments/environment';
import * as fromUser from '../module/users/store/user.reducer';
export interface State {
[fromUser.userFeatureKey]: fromUser.UserState;
}
export const reducers: ActionReducerMap<State> = {
[fromUser.userFeatureKey]: fromUser.reducer
};
export const metaReducers: MetaReducer<State>[] = !environment.production ? [] : [];
app.module.ts
storemodule.forroot(reducers,{metareducers}),
selector.ts
import {createSelector} from '@ngrx/store';
import {UserState} from "./user.reducer";
export const userSelector = createSelector(
(state: UserState) => state.users,
(users) => users
)
我的组件user.component.component.ts
constructor(private store: Store<UserState>) {
this.store.dispatch(loadUsers());
}
ngOnInit(): void {
this.store.pipe(select(userSelector)).subscribe(a => {
console.log("Found " + a);
});
}
redux state ,
{
user: {
selectedUser: null,
users: [
{
id: 3,
email: '[email protected]',
username: 'john.doe',
firstName: 'fsadfad',
middleName: 'sadf',
lastName: 'werew',
accountNonExpired: false,
accountNonLocked: false,
credentialsNonExpired: false,
enabled: true,
verified: false,
description: 'fsdfas',
phone: '0716650714',
lastLogin: null,
fullName: 'Fsadfad sadf werew'
},
{
id: 21,
email: 'erwer',
username: 'sdfsd',
firstName: 'rewrew',
middleName: 'sadfa',
lastName: 'sdfsa',
accountNonExpired: false,
accountNonLocked: false,
credentialsNonExpired: false,
enabled: true,
verified: false,
description: 'sdfds',
phone: 'werw',
lastLogin: null,
fullName: 'Rewrew sadfa sdfsa'
}
],
isLoading: false,
error: null
}
}
但我没有得到数据从选择器。有人可以帮忙吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决了问题。问题是我需要先创建功能选择器
,然后我的selector.ts变为
Solved the problem. the problem was i needed to create feature selector first
Then my selector.ts becomes