关于前端:Spartacus-Storefront-里的-currency-和-language-的-store-设计

7次阅读

共计 1385 个字符,预计需要花费 4 分钟才能阅读完成。

currency 和 language 都长久化在 local storage 里了:

app.config.ts?

Failing E2E tests: The site context tests are failing

波及到的文件:

  • currency-checkout-page.e2e-spec.ts
  • language-checkout-page.e2e-spec.ts

文件门路:projects\storefrontapp-e2e-cypress\cypress\integration\regression\site-context\currency\currency-cart-page.core-e2e-spec.ts

一些相干的函数和文件:

  • default-site-context-config.ts
  • site-context-config-initializer.ts

SiteContext 是一个 interface, 定义了三个办法:

export interface SiteContext<T> {getAll(): Observable<T[]>;
  getActive(): Observable<string>;
  setActive(isocode: string);
}

Site Context 的实现位于 core 文件夹内。选择器是用于获取存储状态切片 (slice) 的纯函数,在 Spartacus 源代码实现中有着宽泛的用处。

store

照例是分了好几个子文件夹。

导入 EffectsModule 和 StoreModule 两个 module:

留神 StoreModule.forFeature 的第二个参数,传入一个 ActionReducerMap 或者 InjectionToken:

在 Spartacus 里,咱们应用的是一个 InjectionToken:

export const reducerToken: InjectionToken<ActionReducerMap<SiteContextState>> =
  new InjectionToken<ActionReducerMap<SiteContextState>>('SiteContextReducers');

token 对应的 providers 在下图第 34 行代码处:

这个 provider 通过工厂函数提供:

在应用程序初始化时就会执行:

在 Angular Routing 和 ngx-bootstrap 的畛域里,forRoot 有助于在指令和组件的多个实例之间共享 providers,以实现应用程序的全局关注。例如,ngx-bootstrap 对模态对话框组件应用此约定。尽管在应用程序的标记中可能定义了许多模态实例,但模态会接管整个 UI.

在路由的状况下,应用程序只有一个 window.location,因而即便可能有子路由和路由器进口组件的各种实例,它们都须要窗口地位的一个全局依赖关系,以便它们能够一起工作。

总之,forRoot() 约定代表了一种应用 ModuleWithProviders 接口导入 NgModule 及其提供程序的办法。

当一个 feature NgModule 导出须要共享雷同自定义提供程序实例的组件和指令时,请思考应用 forRoot() 办法在根 NgModule 中注册这些提供程序。这能够帮忙确保所有子 NgModule 都能够拜访雷同的提供者实例,而无需消费者显式解决提供者注册。

正文完
 0