Angular与ngrx - 没有任何反应
创始人
2024-10-30 18:30:23
0

要解决Angular与ngrx没有任何反应的问题,需要检查以下几个方面:

  1. 确保在应用程序的根模块中正确导入了ngrx模块。例如,在app.module.ts中,确保导入了StoreModule和EffectsModule:
import { StoreModule } from '@ngrx/store';
import { EffectsModule } from '@ngrx/effects';

@NgModule({
  imports: [
    StoreModule.forRoot({}), // 导入store
    EffectsModule.forRoot([]), // 导入effects
  ],
  ...
})
export class AppModule { }
  1. 在组件中,确保使用了正确的ngrx操作符来选择和派发状态。例如,在组件中选择状态:
import { Component, OnInit } from '@angular/core';
import { Store, select } from '@ngrx/store';
import { AppState } from './app-state.model';
import { selectSomeData } from './app.selectors';

@Component({
  ...
})
export class AppComponent implements OnInit {
  someData$ = this.store.pipe(select(selectSomeData));

  constructor(private store: Store) {}

  ngOnInit() {
    // 在初始化时派发一个action
    this.store.dispatch({ type: '[App] Load Data' });
  }
}
  1. 检查ngrx的reducer和effects是否正确配置。例如,在app.reducer.ts中,确保reducers和meta-reducers正确配置:
import { ActionReducerMap, MetaReducer } from '@ngrx/store';
import { someDataReducer } from './some-data.reducer';

export interface AppState {
  someData: any;
}

export const reducers: ActionReducerMap = {
  someData: someDataReducer,
};

export const metaReducers: MetaReducer[] = [];
  1. 检查是否正确使用了ngrx的effects。例如,在app.effects.ts中,检查是否正确导入了Effect和Actions:
import { Injectable } from '@angular/core';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { map, mergeMap } from 'rxjs/operators';
import { DataService } from './data.service';

@Injectable()
export class AppEffects {
  loadData$ = createEffect(() =>
    this.actions$.pipe(
      ofType('[App] Load Data'),
      mergeMap(() =>
        this.dataService.getData().pipe(
          map((data) => ({ type: '[App] Data Loaded', payload: data }))
        )
      )
    )
  );

  constructor(private actions$: Actions, private dataService: DataService) {}
}

确保在根模块中导入并注册了AppEffects:

import { EffectsModule } from '@ngrx/effects';
import { AppEffects } from './app.effects';

@NgModule({
  imports: [
    EffectsModule.forRoot([AppEffects]), // 导入effects
  ],
  ...
})
export class AppModule { }

通过检查以上几个方面,可以找到Angular与ngrx没有任何反应的问题,并修复代码示例。

相关内容

热门资讯

透视黑科技!aapoker免费... 透视黑科技!aapoker免费透视脚本(透视)透视脚本(竟然是真的有挂)1、点击下载安装,aapok...
辅助透视!aapoker怎么提... 辅助透视!aapoker怎么提高中牌率(透视)插件下载(本来真的是有挂)1、aapoker怎么提高中...
透视了解!aapoker辅助软... 透视了解!aapoker辅助软件合法吗(透视)脚本(原来真的是有挂)1、任何aapoker辅助软件合...
透视工具!aapoker辅助器... 透视工具!aapoker辅助器怎么用(透视)透视脚本入口(真是真的是有挂);1、aapoker辅助器...
透视app!aapoker辅助... 透视app!aapoker辅助怎么用(透视)辅助(一贯存在有挂)1、aapoker辅助怎么用系统规律...
透视辅助!aapoker俱乐部... 透视辅助!aapoker俱乐部靠谱吗(透视)插件(本来真的有挂)aapoker俱乐部靠谱吗辅助器中分...
透视安卓版!aapoker怎么... 透视安卓版!aapoker怎么开辅助器(透视)透视插件(果然是有挂)1、aapoker怎么开辅助器透...
透视肯定!aapoker辅助器... 透视肯定!aapoker辅助器怎么用(透视)辅助(竟然是真的有挂)1、实时aapoker辅助器怎么用...
透视安卓版!aapoker透视... 透视安卓版!aapoker透视脚本入口(透视)插件(确实是真的有挂)aapoker透视脚本入口辅助器...
透视最新!aapoker安装包... 透视最新!aapoker安装包怎么使用(透视)辅助插件工具(其实真的有挂)亲,关键说明,aapoke...