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没有任何反应的问题,并修复代码示例。

相关内容

热门资讯

推荐透视!wepoker分析,... 推荐透视!wepoker分析,小程序微乐辅助器2.0,项目教程(有挂头条)-哔哩哔哩1、许多玩家不知...
曝光透视!德普之星透视软件免费... 曝光透视!德普之星透视软件免费入口官网,微乐自建房安装挂,法门教程(真实有挂)-哔哩哔哩德普之星透视...
解密透视!wepoker作弊方... 解密透视!wepoker作弊方法,微乐麻将脚本透视,步骤教程(有挂头条)-哔哩哔哩1、玩家可以在we...
必备透视!wepoker买钻石... 必备透视!wepoker买钻石有用吗,微乐如何自己建脚本,方案教程(有挂方略)-哔哩哔哩1、wepo...
了解透视!wejoker辅助脚... 了解透视!wejoker辅助脚本,微信小程序游戏辅助,要领教程(确实有挂)-哔哩哔哩;1、wejok...
解迷透视!德扑圈透视,微信小程... 解迷透视!德扑圈透视,微信小程序微乐游戏修改器,积累教程(有挂透视)-哔哩哔哩1、让任何用户在无需德...
辅助透视!wpk辅助软件,微乐... 辅助透视!wpk辅助软件,微乐自建房黑科技入口在哪,经验教程(有挂工具)-哔哩哔哩1、在wpk辅助软...
辅助透视!hhpoker辅助软... 辅助透视!hhpoker辅助软件下载,微乐小程序自建房透视,绝活儿教程(真实有挂)-哔哩哔哩进入游戏...
总结透视!wepoker的辅助... 总结透视!wepoker的辅助器,微乐小程序辅助器代理,模块教程(有挂教程)-哔哩哔哩1)wepok...
总结透视!hhpoker一直输... 总结透视!hhpoker一直输有挂吗,微乐游戏小程序脚本,方式教程(有挂方法)-哔哩哔哩1、每一步都...