【问题标题】:One NGRX store, multiple Angular Applications一个 NGRX 商店,多个 Angular 应用程序
【发布时间】:2021-01-20 15:40:10
【问题描述】:

所以我对此有点不以为然。希望你能向我道歉。

我正在尝试将多个 Angular 元素(Web 组件)插入到 Prestashop 模块中,即 PHP。由于没有一个包含许多模块的 Angular 应用程序,我无法使用商店将数据从一个元素发送到另一个元素。

我曾想过创建一个库来负责创建 NGRX 存储,并将其保存到 window.store(例如),以便多个独立的 Angular 应用程序可以使用同一个存储。这里的问题是我必须在许多 Angular 元素中调用库,所以我在构造函数中注入了不同的存储。我说的对吗?

例如:

import {
  ActionReducer,
  ActionReducerFactory,
  ActionReducerMap,
  ActionReducerMap,
  ActionsSubject,
  ReducerManager,
  ReducerManagerDispatcher,
  StateObservable,
  Store,
} from '@ngrx/store'
import { Observable } from 'rxjs';

const storeName = 'store'

@Injectable({
  providedIn: 'root'
})
export class StoreSyncService {

  store: any = null

  // option 1 constructor:
  // are we, when including the library in others angular apps, instantiating store over and over?
  constructor(private store: Store) {
     if (window[storeName]) {
      this.store = window[storeName]
    } else{
      window[storeName] = this.store
    }
  }

  // option 2 constructor (please apologise me as I don't know how injection works very well).
  // Any link to clarify this is welcome
  constructor() {
    if (window[storeName]) {
      this.store = window[storeName]
    } else{
      const stateObservable$: StateObservable = new Observable()
      const actionsObserver: ActionsSubject = new ActionsSubject()

      const dispatcher: ReducerManagerDispatcher = new ActionsSubject()
      const initialState = {}
      const reducers: ActionReducerMap<any, any> = {}
      const reducerFactory: ActionReducerFactory<any, any> =  whatever

      const reducerManager: ReducerManager = new ReducerManager(dispatcher, initialState, reducers, reducerFactory)
      this.store = new Store(stateObservable$, actionsObserver, reducerManager)
      window[storeName] = this.store
    }
  }

  public getStore = () => this.store

  public addReducer = (reducerName: string, reducer: ActionReducer<any>) => this.store.addReducer(reducerName, reducer)

  public removeReducer = (reducerName: string) => this.store.removeReducer(reducerName)

}

回顾一下,如果我想在多个独立的 Angular 应用程序之间共享状态,你会怎么做?

我尝试使用 localStorage,添加事件以将 NGRX 状态与本地存储中的数据同步,反之亦然,但有时会导致无限循环,有时会出现数据不同步的边缘情况。

感谢您的帮助和理解

【问题讨论】:

    标签: javascript angular ngrx store ngrx-store


    【解决方案1】:

    所以现在,我找到了一种解决方法来避免商店的双重实例化。使用条件注入器。

      import { Injectable, Injector } from '@angular/core';
    
      ...
    
    
      constructor(private injector : Injector) {
        if (window[storeName]) {
          this.store = window[storeName]
        } else{
          this.store = this.injector.get<Store>(Store);
          window[storeName] = this.store
        }
      }
    

    我还没有测试它在独立应用程序之间共享数据。让我们希望它有效。

    欢迎任何建议!!

    【讨论】:

    • 我正在尝试解决同样的问题。我很想知道这种方法对您有何帮助。
    • 嘿@tkooser 所以它工作得很好,但请注意,根据你想要做什么,你可能需要创建自己的效果/动作等等......已经有一段时间了自从我这样做了,我最终分叉了 NGRX repo 并使其适应我的需要。所以请继续这样做,如果您需要任何帮助,请随时询问
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-11
    • 2019-01-24
    相关资源
    最近更新 更多