我如何从ngxs中的另一个组件获取状态值
how can i get state value from another component in ngxs
common.state.ts
import { Action, Selector, State, StateContext } from '@ngxs/store';
import { NzMessageService } from 'ng-zorro-antd';
import { PermissionDict } from '../../model/organization-authority/setting/permission-dict.model';
import { CommonService } from '../../service/common.service';
import {GetIsSuperAdminAction} from '../action/common.action';
import { map } from 'rxjs/operators';
import { Injectable } from '@angular/core';
export interface CommonStateModel {
superAdminState: number;
}
@State<CommonStateModel>({
name: '_CommonState',
defaults: {
superAdminState: null
}
})
@Injectable()
export class CommonState {
constructor(
private _commonService: CommonService,
) {}
@Selector()
public static superAdminState(state: CommonStateModel) {
console.log(state.superAdminState + 'A');
//is not execute
return state.superAdminState;
}
/**
* @param ctx
* @param action
*/
@Action(GetIsSuperAdminAction)
GetIsSuperAdminAction(ctx: StateContext<CommonStateModel>, action: GetIsSuperAdminAction) {
console.log(action.superAdminState);
const state = ctx.getState();
ctx.setState({ ...state, superAdminState: action.superAdminState });
}
}
layout.component.ts:
onClick(){
this._store.dispatch(new GetIsSuperAdminAction(Math.random()));
}
工人-dashboard.component.ts
superAdminState: Observable<number>;
this.superAdminState = this._store.select(CommonState.superAdminState);
工人-dashboard.component.html
{{ superAdminState | async }}
我无法获取 superAdminState 状态值,因为 superAdminState 显示在 html 值未定义
superAdminState value is undefined
action changed the state value
您的布局组件的 onClick 方法没有问题,它将调度您设置状态所需的操作。缺少的是您还需要在此组件中使用选择器。 @Select(CommonState.superAdminState) superAdminState: Observable<any>;
.
common.state.ts
import { Action, Selector, State, StateContext } from '@ngxs/store';
import { NzMessageService } from 'ng-zorro-antd';
import { PermissionDict } from '../../model/organization-authority/setting/permission-dict.model';
import { CommonService } from '../../service/common.service';
import {GetIsSuperAdminAction} from '../action/common.action';
import { map } from 'rxjs/operators';
import { Injectable } from '@angular/core';
export interface CommonStateModel {
superAdminState: number;
}
@State<CommonStateModel>({
name: '_CommonState',
defaults: {
superAdminState: null
}
})
@Injectable()
export class CommonState {
constructor(
private _commonService: CommonService,
) {}
@Selector()
public static superAdminState(state: CommonStateModel) {
console.log(state.superAdminState + 'A');
//is not execute
return state.superAdminState;
}
/**
* @param ctx
* @param action
*/
@Action(GetIsSuperAdminAction)
GetIsSuperAdminAction(ctx: StateContext<CommonStateModel>, action: GetIsSuperAdminAction) {
console.log(action.superAdminState);
const state = ctx.getState();
ctx.setState({ ...state, superAdminState: action.superAdminState });
}
}
layout.component.ts:
onClick(){
this._store.dispatch(new GetIsSuperAdminAction(Math.random()));
}
工人-dashboard.component.ts
superAdminState: Observable<number>;
this.superAdminState = this._store.select(CommonState.superAdminState);
工人-dashboard.component.html
{{ superAdminState | async }}
我无法获取 superAdminState 状态值,因为 superAdminState 显示在 html 值未定义
superAdminState value is undefined
action changed the state value
您的布局组件的 onClick 方法没有问题,它将调度您设置状态所需的操作。缺少的是您还需要在此组件中使用选择器。 @Select(CommonState.superAdminState) superAdminState: Observable<any>;
.