ngrx选择返回存储不是值

发布于 2025-01-24 10:18:19 字数 4746 浏览 0 评论 0原文

catalogueselection in Store Image

我在state(catalogueSelection)中需要数据并需要将字符串的“ searchTextresult”传递到一个组件中,而“ andaray”类别“ checkboxresult”中。

当我尝试检索所需的值时,我正在检索整个商店。我在这里查看了许多网站和条目,但现在变得非常困惑。

模型:

export class SearchTextResult {
  searchTextResult: string;
}

export class CategoryCheckboxResult {
  categoryCheckboxResult:Array<CategoryCheckboxResult>;
}

操作:

import { Action } from '@ngrx/store';
import { SearchTextResult, CategoryCheckboxResult } from 'app/@core/services/products/products.model';

export enum UserCatalogueSelectionTypes {
    AddSearchTextResult = '[SearchTextResult] AddResult',
    AddCategoryCheckboxResult = '[CategoryCheckboxResult] AddResult',
    GetSearchTextResult = '[SearchTextResult] GetResult',
}

export class AddSearchTextResult implements Action {
    readonly type = UserCatalogueSelectionTypes.AddSearchTextResult;
    constructor(public payload: SearchTextResult){
    }
}

export class AddCategoryCheckboxResult implements Action {
    readonly type = UserCatalogueSelectionTypes.AddCategoryCheckboxResult;
    constructor(public payload: CategoryCheckboxResult){
    }
}

export class GetSearchTextResult implements Action {
    readonly type = UserCatalogueSelectionTypes.GetSearchTextResult;
}

export type UserCatalogueSelectionUnion =
| AddSearchTextResult
| AddCategoryCheckboxResult
| GetSearchTextResult

缩小器:

import { SearchTextResult, CategoryCheckboxResult} from "app/@core/services/products/products.model";
import { UserCatalogueSelectionTypes,  UserCatalogueSelectionUnion} from "../actions/products.actions";


export interface UserCatalogueSelectionState {
 searchTextResult: SearchTextResult | null;
 categoryCheckboxResult: CategoryCheckboxResult | null;
}

export const initialState: UserCatalogueSelectionState = {
    searchTextResult: null,
    categoryCheckboxResult: null,
}

 
export function reducer(state:UserCatalogueSelectionState = initialState, action: UserCatalogueSelectionUnion ): UserCatalogueSelectionState{
    switch (action.type) {
        case UserCatalogueSelectionTypes.AddSearchTextResult: 
            return {
                ...state, 
                searchTextResult: action.payload,
            };
        
        case UserCatalogueSelectionTypes.AddCategoryCheckboxResult: 
            return {
                ...state, 
                categoryCheckboxResult: action.payload,
            };

        case UserCatalogueSelectionTypes.GetSearchTextResult: {
            return state;
        }
            
        default: {
            return state;
        }

    }
}

选择器:

import { createSelector,createFeatureSelector } from "@ngrx/store";
import {UserCatalogueSelectionState} from '../../store/reducer/products.reducer';

export const fetchSearchTextResults = createFeatureSelector<UserCatalogueSelectionState>("searchTextResult");

export const fetchSearchTextResult = createSelector (
    fetchSearchTextResults,
    (state:UserCatalogueSelectionState) => state.searchTextResult.searchTextResult
);

export const fetchCatalogueCheckBoxResults = createFeatureSelector<UserCatalogueSelectionState>("catalogueCheckboxResult");

export const fetchCatalogueCheckBoxResult = createSelector (
    fetchCatalogueCheckBoxResults,
    (state: UserCatalogueSelectionState) => state.categoryCheckboxResult.categoryCheckboxResult
);

我的组件1

可观察:

public searchTextResult: Observable<String>;

contructor:(一部分)

private store: Store<fromCatalogueSelection.UserCatalogueSelectionState>

代码代码段:(询问数据)

this.searchTextResult = this.store.select('SearchTextResult');
console.log('TESTING SEARCH TEXT: ', this.searchTextResult);

控制台:

测试搜索文本:商店&nbsp; {_ isscalar:false,actionsobserver:actionsSubject,reducerManager:&gt; reducerManager,来源:商店,operator:dimpptionuntilChangeDoperator}

我的组件2

可观察的代码2可观察的

searchTextResult$: Observable<CatalogueSelectionActions.GetSearchTextResult>;

代码smippet:(询问数据)

this.searchTextResult$ = this.store.select('GetSearchTextResult');
console.log('TESTING SEARCH TEXT: ', this.searchTextResult$);

控制台:

测试搜索文本:商店&nbsp; {_ isscalar:false,actionsobserver:actionsSubject,reducerManager:&gt; Reducermanager,来源:商店,操作员:独特的nuntilChangedOperator}

目前我已经放弃了选择器上的独特努力。当我要盘旋时,任何帮助都非常感谢。

catalogueSelection in Store Image

I have the data I require in state (catalogueSelection: searchTextResult & categoryCheckboxResult) and need to pass the string 'SearchTextResult' into one component and the Array 'categoryCheckboxResult' into another.

When I try to retrieve the required values I am retrieving the whole store. I have looked at numerous websites and entries here but getting very confused now.

Model:

export class SearchTextResult {
  searchTextResult: string;
}

export class CategoryCheckboxResult {
  categoryCheckboxResult:Array<CategoryCheckboxResult>;
}

Actions:

import { Action } from '@ngrx/store';
import { SearchTextResult, CategoryCheckboxResult } from 'app/@core/services/products/products.model';

export enum UserCatalogueSelectionTypes {
    AddSearchTextResult = '[SearchTextResult] AddResult',
    AddCategoryCheckboxResult = '[CategoryCheckboxResult] AddResult',
    GetSearchTextResult = '[SearchTextResult] GetResult',
}

export class AddSearchTextResult implements Action {
    readonly type = UserCatalogueSelectionTypes.AddSearchTextResult;
    constructor(public payload: SearchTextResult){
    }
}

export class AddCategoryCheckboxResult implements Action {
    readonly type = UserCatalogueSelectionTypes.AddCategoryCheckboxResult;
    constructor(public payload: CategoryCheckboxResult){
    }
}

export class GetSearchTextResult implements Action {
    readonly type = UserCatalogueSelectionTypes.GetSearchTextResult;
}

export type UserCatalogueSelectionUnion =
| AddSearchTextResult
| AddCategoryCheckboxResult
| GetSearchTextResult

Reducers:

import { SearchTextResult, CategoryCheckboxResult} from "app/@core/services/products/products.model";
import { UserCatalogueSelectionTypes,  UserCatalogueSelectionUnion} from "../actions/products.actions";


export interface UserCatalogueSelectionState {
 searchTextResult: SearchTextResult | null;
 categoryCheckboxResult: CategoryCheckboxResult | null;
}

export const initialState: UserCatalogueSelectionState = {
    searchTextResult: null,
    categoryCheckboxResult: null,
}

 
export function reducer(state:UserCatalogueSelectionState = initialState, action: UserCatalogueSelectionUnion ): UserCatalogueSelectionState{
    switch (action.type) {
        case UserCatalogueSelectionTypes.AddSearchTextResult: 
            return {
                ...state, 
                searchTextResult: action.payload,
            };
        
        case UserCatalogueSelectionTypes.AddCategoryCheckboxResult: 
            return {
                ...state, 
                categoryCheckboxResult: action.payload,
            };

        case UserCatalogueSelectionTypes.GetSearchTextResult: {
            return state;
        }
            
        default: {
            return state;
        }

    }
}

Selectors:

import { createSelector,createFeatureSelector } from "@ngrx/store";
import {UserCatalogueSelectionState} from '../../store/reducer/products.reducer';

export const fetchSearchTextResults = createFeatureSelector<UserCatalogueSelectionState>("searchTextResult");

export const fetchSearchTextResult = createSelector (
    fetchSearchTextResults,
    (state:UserCatalogueSelectionState) => state.searchTextResult.searchTextResult
);

export const fetchCatalogueCheckBoxResults = createFeatureSelector<UserCatalogueSelectionState>("catalogueCheckboxResult");

export const fetchCatalogueCheckBoxResult = createSelector (
    fetchCatalogueCheckBoxResults,
    (state: UserCatalogueSelectionState) => state.categoryCheckboxResult.categoryCheckboxResult
);

My Component 1

Observable:

public searchTextResult: Observable<String>;

Contructor: (part of)

private store: Store<fromCatalogueSelection.UserCatalogueSelectionState>

Code Snippet: (asking for the data)

this.searchTextResult = this.store.select('SearchTextResult');
console.log('TESTING SEARCH TEXT: ', this.searchTextResult);

Console:

TESTING SEARCH TEXT: Store {_isScalar: false, actionsObserver: ActionsSubject, reducerManager: >ReducerManager, source: Store, operator: DistinctUntilChangedOperator}

My Component 2

Observable

searchTextResult$: Observable<CatalogueSelectionActions.GetSearchTextResult>;

Code Snippet: (asking for the data)

this.searchTextResult$ = this.store.select('GetSearchTextResult');
console.log('TESTING SEARCH TEXT: ', this.searchTextResult$);

Console:

TESTING SEARCH TEXT: Store {_isScalar: false, actionsObserver: ActionsSubject, reducerManager: > ReducerManager, source: Store, operator: DistinctUntilChangedOperator}

I've given up on the Selectors for the moment. Any help much appreciated as I'm going a round in circles.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呆橘 2025-01-31 10:18:19

您几乎在那里,控制台日志的值是可观察的对象,每次从商店中选择某些东西时,都会在可观察到的中获得包装的值。您只需要订阅

this.searchTextResult$ = this.store.select('GetSearchTextResult');
this.searchTextResult$.subscribe((yourData) => console.log(yourData));

另外,由于您正在使用选择器,请使用它们,因此您不必编写状态/选择器名称:

Selector

...
export const fetchCatalogueCheckBoxResult = createSelector (
    fetchCatalogueCheckBoxResults,
    (state: UserCatalogueSelectionState) => 
      state.categoryCheckboxResult.categoryCheckboxResult
);

组件

import * as YourSelectors from './store/something/selectors/yourthing.selectors'

...
...
this.searchTextResult$ = this.store
    .select(YourSelectors.fetchCatalogueCheckBoxResult)
    .subscribe(console.log);

此外,尝试使用使用async管道将其委派给模板html,以便您不必交易例如,代码中的订阅:

组件

...
export class Component {
  searchTextResult$!: Observable<any> // your data type here
...
...
this.searchTextResult$ = this.store
    .select(YourSelectors.fetchCatalogueCheckBoxResult)
} 

html

<ng-container *ngIf="(searchTextResult$ | async) as result">
  <p>Your result value: {{ result }}</p>
</ng-container>

You are almost there, the value from the console log is the observable object, everytime you select something from the store, you will get the value wrapped within an observable. You just need to subscribe to it:

this.searchTextResult$ = this.store.select('GetSearchTextResult');
this.searchTextResult$.subscribe((yourData) => console.log(yourData));

Also, since you are working with selectors, use them, you don't have to write the state/selector name:

selector

...
export const fetchCatalogueCheckBoxResult = createSelector (
    fetchCatalogueCheckBoxResults,
    (state: UserCatalogueSelectionState) => 
      state.categoryCheckboxResult.categoryCheckboxResult
);

component

import * as YourSelectors from './store/something/selectors/yourthing.selectors'

...
...
this.searchTextResult$ = this.store
    .select(YourSelectors.fetchCatalogueCheckBoxResult)
    .subscribe(console.log);

Additionally, try to subscribe using the async pipe delegating that to your template html so you don't have to deal with the subscription in the code, for example:

component

...
export class Component {
  searchTextResult$!: Observable<any> // your data type here
...
...
this.searchTextResult$ = this.store
    .select(YourSelectors.fetchCatalogueCheckBoxResult)
} 

html

<ng-container *ngIf="(searchTextResult$ | async) as result">
  <p>Your result value: {{ result }}</p>
</ng-container>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文