我正在尝试使用以下方法检索状态信息:
this.store.select(state => console.log(state));
不幸的是,我在某个地方犯了错误,我不知道在哪里,如果有人可以看到他,我将不胜感激。
下面,我将放置错误所在的代码,在此先感谢您提供的所有帮助。
[动作]:
import { IWindowParameters } from "../window-parameters.interface";
/* NgRx */
import { Action } from '@ngrx/store';
export enum AppWeatherActionTypes {
SetCurrentWindowParameters = "[App] Set Current Window Parameters"
}
// Action Creators
export class SetCurrentWindowParameters implements Action {
readonly type: string = AppWeatherActionTypes.SetCurrentWindowParameters;
constructor(public payload: IWindowParameters) { }
}
export type AppWeatherActions = SetCurrentWindowParameters
[减速器]:
/* NgRx */
import { createFeatureSelector, createSelector } from '@ngrx/store';
import * as fromAction from "../actions/app-weather.action";
import { IWeatherAppState } from "../state/app-weather.state";
export const initialAppState: IWeatherAppState = {
windowParameters: null
};
export function appReducer(state = initialAppState, action: fromAction.AppWeatherActions): IWeatherAppState {
switch (action.type) {
case fromAction.AppWeatherActionTypes.SetCurrentWindowParameters:
return { ...state, windowParameters: { ...action.payload } }
default:
return state;
}
}
[状态]
import { IWindowParameters } from "../window-parameters.interface";
export interface IWeatherAppState {
windowParameters: IWindowParameters;
}
[app.module.ts]
/* NgRx */
import { StoreModule } from '@ngrx/store';
import { StoreDevtoolsModule } from '@ngrx/store-devtools';
import { environment } from '../environments/environment';
import { appReducer } from "./reducers/app-weather.reducer";
@NgModule({
declarations: [
AppWeather,
WeatherMainScreen,
WeatherView,
WeatherPlugins,
WeatherInfoPanel,
MyHighlightDirective
],
imports: [
BrowserModule,
AppRoutingModule,
StoreModule.forRoot({ windowParameters: appReducer}),
StoreDevtoolsModule.instrument({
name: 'Weather Radar App DevTools',
maxAge: 25,
logOnly: environment.production,
})
],
providers: [
WindowParametersService],
bootstrap: [AppWeather]
})
export class AppModule { }
答案 0 :(得分:1)
select
函数会从您的状态中获取一个切片,该切片可以是字符串或对象。然后,该函数返回一个Observable
,您可以subscribe
向其返回。像这样:
this.store.select('pizzas').subscribe(console.log);