当前位置:网站首页>SAP 电商云 Spartacus UI Store 相关的设计明细
SAP 电商云 Spartacus UI Store 相关的设计明细
2022-07-20 20:18:00 【汪子熙】
Store
state.ts
定义了和 Site Context 业务相关的 State 数据模型。
定义数据模型的套路是:
export const SITE_CONTEXT_FEATURE = 'siteContext';
export interface StateWithSiteContext {
[SITE_CONTEXT_FEATURE]: SiteContextState;
}
这是最顶层的 State 模型了。
SiteContextState 包含了三个子 State:
export interface SiteContextState {
languages: LanguagesState;
currencies: CurrenciesState;
baseSite: BaseSiteState;
}
以 CurrenciesState 为例,不仅包含了 Entities 列表,还包含了当前 Active 状态的 Currency:
export interface CurrenciesState {
entities: CurrencyEntities;
activeCurrency: string;
}
再定义 Entities 列表:
export interface CurrencyEntities {
[isocode: string]: Currency;
}
以上就是 Site Context 领域所需的 State 数据结构。
注意 SITE_CONTEXT_FEATURE 的使用场合,除了本文件定义 feature state 之外,还用在下列两个文件内:
场景1:用来创建 feature selector:
场景2:使用 StoreModule.forFeature 注册 store:
当使用 createSelector 和 createFeatureSelector 函数时,@ngrx/store 会跟踪调用选择器函数的最新参数。 因为选择器是纯函数,所以当参数匹配时可以返回最后一个结果,而无需重新调用选择器函数。 这可以提供性能优势,特别是对于执行昂贵计算的选择器。这种做法称为 memoization
.
createFeatureSelector 是返回顶级(Top Level
)的 Feature State
的便捷方法。 它为状态的特征切片(Feature Slice
)返回一个类型化(typed
)的选择器函数。
注意 createFeatureSelector 的调用有两种写法。
写法1
下图 2 必须是 1 的一个切片,并且 3 的类型必须和 2 的类型一致:
2 的位置其实就是 result 的位置:
写法2
import {
createSelector, createFeatureSelector } from '@ngrx/store';
export const featureKey = 'feature';
export interface FeatureState {
counter: number;
}
export interface AppState {
feature: FeatureState;
}
export const selectFeature = createFeatureSelector<AppState, FeatureState>(featureKey);
export const selectFeatureCount = createSelector(
selectFeature,
(state: FeatureState) => state.counter
);
我做过测试,在 SAP 电商云 Spartacus UI 项目里,两种写法完全等价:
可以顺利通过编译:
边栏推荐
猜你喜欢
Learning notes (1) getting to know uni app for the first time
ASEMI整流桥MB10M参数,MB10M大小,MB10M特性
当删则删,这种电容本不该出现
The simplest implementation of throttling and anti chattering
Rt-thread-2022 summer camp - learning summary - day 3 (thread synchronization)
阿里云原生应用平台架构师田伟:应用架构的规划、治理与演进
【硬件基础4】二极管(原理、特性、类型、电路分析)
互联网行业的中年危机是35岁这分水岭
学习笔记(1)初识uni-app
全栈开发实战 | SSM框架整合完整教程
随机推荐
读书会 | 拨开心理咨询的迷雾:普通人应该如何看待心理咨询
[错题分析]方格取数
(PC+WAP)织梦模板淀粉类网站
高等数学(第七版)同济大学 习题3-2 个人解答
do-while 怎么调用编写的 odps sql 呢?
ARM汇编指令集·带例题
conda升级tensorflow-gpu=2.5.0及cudn,cudatoolkit版本
MiniProg3进行Hex烧录
Rt-thread-2022 summer camp - learning summary - the second day
【JS】你不知道的 console 命令
SQL语句
Awk statistical difference record
性能提升30倍丨基于 DolphinDB 的 mytt 指标库实现
在node.js项目中安装配置mysql模块并进行增删改查
招募 | “壹脑云科研圈“招募新成员啦
全棧開發實戰 | SSM框架整合完整教程
July 2022 Russian database ranking: Clickhouse ranked first and gigabase ranked second
ASEMI整流桥MB10M参数,MB10M大小,MB10M特性
数据库自增ID用完了会怎样?
乘数科技云管控平台适配阿里云PolarDB,共促云原生数据库生态繁荣