简体   繁体   English

在升级到9.0和角度7后修复angular-redux / store

[英]Fix angular-redux/store after upgrade to 9.0 and angular 7

I have taken over an existing angular application which I am in the process of updating to version 7 from 5. For state-management, it is using @angular-redux which was updated to version 9.0. 我接管了一个现有的角度应用程序,我正在从5更新到版本7.对于状态管理,它使用@ angular-redux更新到版本9.0。 However keeping the store as is has caused the app to not load (with no console errors). 但是,按原样保留存储会导致应用程序无法加载(没有控制台错误)。

I have changed the function in each individual epic as shown in the code as told do in the terminal. 我已经改变了每个单独史诗中的功能,如终端中告知的代码所示。 However I have not found a resource as to how to adapt the main store.ts file that the project is running, which is breaking the app. 但是我没有找到关于如何调整项目正在运行的主store.ts文件的资源,这正在破坏应用程序。 If anyone knows what to put in that file, to make the store be set up appropriately, please do share. 如果有人知道该放入该文件的内容,为了使商店得到适当的设置,请分享。

store.ts: store.ts:

import { NgModule } from "@angular/core";
import {
  NgReduxModule,
  NgRedux,
  DevToolsExtension
} from "@angular-redux/store";
import { NgReduxRouterModule, NgReduxRouter } from "@angular-redux/router";
// The top-level reducers and epics that make up our app's logic.
import { IModel, initialData } from "./model";
import { rootReducer } from "./reducer";

import { Epic } from "./epic";
import { StorageService } from "./storage-service";
// DataModels
import { ApplicationStateModule } from "./application-state/module";
import { EntitiesModule } from "./entities/module";
import { LiveDataModule } from "./live-data/module";
import { RouterModule } from "./router/module";
import { StoreSelectors } from "./selectors";
import { TimePeriodModule } from "./time-period/module";
import { TimeSeriesDataModule } from "./time-series-data/module";
import { SelectorsModule } from "../selectors/selectors.module";
import { PointInTimeModule } from "./point-in-time/module";
import { applyMiddleware, createStore } from "redux";
import { createEpicMiddleware } from "redux-observable";

@NgModule({
  imports: [
    NgReduxModule,
    NgReduxRouterModule.forRoot(),
    ApplicationStateModule,
    EntitiesModule,
    LiveDataModule,
    RouterModule,
    ...Module,
    ...Module,
    ...Module,
    ...Module
  ],
  providers: [Epic, StoreSelectors, StorageService]
})
export class Store {
  constructor(
    public store: NgRedux<IModel>,
    devTools: DevToolsExtension,
    ngReduxRouter: NgReduxRouter,
    rootEpic: Epic,
    storageService: StorageService
  ) {
    store.configureStore(
      rootReducer,
      storageService.fromStore(initialData),
// the commented code below breaks the app if either line is uncommented
      // [...rootEpic.createEpics()],

      // devTools.isEnabled() ? [devTools.enhancer()] : []
    );
    if (ngReduxRouter) {
      ngReduxRouter.initialize();
    }
  }
}

Here is an example of one of the specific epic.ts files: 以下是其中一个特定epic.ts文件的示例:

import { Injectable } from '@angular/core';
import { Actions } from './actions';
import { Actions as ApplicationStateActions } from '../applicationstate/actions';
import { createEpicMiddleware } from 'redux-observable';
import { createStore, applyMiddleware } from 'redux';
import { rootReducer } from '../reducer';

@Injectable()
export class Epic {
  constructor(
    private actions: Actions,
    private applicationStateActions: ApplicationStateActions
  ) {}

  load = (action$) =>
    action$
    .ofType(Actions.SET_DATES_AVAILABLE)
      .map((action) => {
        return this.actions.setUnit(2);
      })
    .ofType(Actions.SET_UNIT)
      .map((action, state) => {
        return this.applicationStateActions.updateTimePeriodStatus('ready');
      })

  public createEpics() {
  const epicMiddleware = createEpicMiddleware();
  const store = createStore(rootReducer, applyMiddleware(epicMiddleware));
// tslint:disable-next-line: no-void-expression
 return [epicMiddleware.run(this.load)];

//below is the original way it was, above is the change the terminal said to do,
//see https://github.com/redux-observable/redux-observable/blob/master/MIGRATION.md

// return [createEpicMiddleware(this.load)];

  }
}

In case it helps anyone, this is the final way the code looked in order to work. 如果它可以帮助任何人,这是代码工作的最终方式。

RootEpic.ts: RootEpic.ts:

import { Injectable } from "@angular/core";
import { Epic as xxxxxStateEpic } from "./application-state/epic";
import { Epic as bbbbbbEpic } from "./entities/epic";
import { Epic as ggggggEpic } from "./live-data/epic";
import { Epic as fffffEpic } from "./time-period/epic";
import { Epic as dddddDataEpic } from "./time-series-data/epic";
import { Epic as qqqqqqEpic } from "./point-in-time/epic";

@Injectable()
export class RootEpic {
  constructor(
    private xxxxxStateEpic: xxxxxStateEpic,
    private bbbbEpic: bbbbbEpic,
    private gggggEpic: gggggEpic,
    private fffffEpic: ffffffEpic,
    private ddddddData: ddddddEpic,
    private qqqqqEpic: qqqqqqqEpic
  ) {}

  public createEpics() {
    return [
      ...this.gggggEpic.createEpics(),
      ...this.bbbbbbEpic.createEpics(),
      ...this.fffffffEpic.createEpics(),
      ...this.xxxxxStateEpic.createEpics(),
      ...this.qqqqqqqEpic.createEpics(),
      ...this.dddddData.createEpics()
    ];
  }
}

each specific epic ended in: 每个特定的史诗结束于:

myEpic = (action$, state$) =>
...... end of myEpic,

 createEpics(){
return [this.myEpic];
}

finally the store.ts file should look like this 最后store.ts文件看起来应该是这样的

@NgModule({
  imports: [
    NgReduxModule,
    NgReduxRouterModule.forRoot(),
   ...(list out modules here)
  ],
  providers: [RootEpic, StoreSelectors, StorageService]
})
export class Store {
  constructor(
    public store: NgRedux<IModel>,
    devTools: DevToolsExtension,
    ngReduxRouter: NgReduxRouter,
    rootEpic: RootEpic,
    storageService: StorageService
  ) {
    const epicMiddleware = createEpicMiddleware();

    store.configureStore(
      rootReducer,
     storageService.fromStore(initialData),
     [epicMiddleware],
      devTools.isEnabled() ? [devTools.enhancer()] : []
    );

    epicMiddleware.run(combineEpics(...rootEpic.createEpics()));
    if (ngReduxRouter) {
      ngReduxRouter.initialize();
    }
  }
}

Enjoy!! 请享用!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Storybook中使用@ angular-redux / store测试Angular - Testing Angular with @angular-redux/store in Storybook 带有 angular-redux/store 的 angular Guard - angular guard with angular-redux/store 使用@ angular-redux / store测试@select装饰器 - Testing @select decorators with @angular-redux/store @angular-redux/store:如何为 redux 商店编写单元测试? - @angular-redux/store : How to write unit test for the redux store? 如何在angular-redux / store中初始化根存储以进行测试? - How to initialize root store in angular-redux/store for testing? Angular-Redux设计-数组选择器 - Angular-redux design - array selector 如何单元测试从 angular 10 中的“@angular-redux/store”中选择? - how to unit test select from "@angular-redux/store" in angular 10? 测试 Angular 组件,该组件引入了一个派生自 @angular-redux/store 的“@select()”方法的 observable - Test Angular component that brings in an observable derived from @angular-redux/store's “@select()” method 带有ng2-redux的Angular 4.4测试服务(angular-redux) - Angular 4.4 testing service with ng2-redux (angular-redux) @angular-redux/store 尝试测试在用 @select() 装饰的属性上使用异步的组件 - 文档不存在 - @angular-redux/store trying to test component that uses async on a property decorated with @select() - documentation does not exist
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM