所以我對此有點不以為然。希望你能給我道歉。我正在嘗試將多個 Angular 元素(Web 組件)插入到 Prestashop 模塊,即 PHP。由于沒有一個包含許多模塊的 Angular 應用程序,我無法使用商店將數據從一個元素發送到另一個元素。我想過創建一個負責創建 NGRX 商店的庫,并將其保存到 window.store(例如),因此同一個商店可以被多個獨立的角度應用程序使用。這里的問題是我必須在許多 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 }所以回顧一下,如果我想在多個獨立的角度應用程序之間共享狀態,你會怎么做?我嘗試使用 localStorage,添加事件以將 NGRX 狀態與本地存儲中的數據同步,反之亦然,但有時會出現無限循環,有時會出現數據不同步的邊緣情況。感謝您的幫助和理解
一個 NGRX 商店,多個 Angular 應用程序
慕碼人2483693
2023-04-20 10:01:31