天天看點

SAP 電商雲 Spartacus UI 和路由相關的 State 處理

state,effects,action,reducer 都在這個檔案夾裡:

SAP 電商雲 Spartacus UI 和路由相關的 State 處理
在 routing-state.ts 裡定義了 RouterState 接口:
SAP 電商雲 Spartacus UI 和路由相關的 State 處理
繼承自 ngrx router 裡的 RouterReducerState 類型,類型參數為我們自定義的 ActivatedRouterStateSnapshot.

export interface ActivatedRouterStateSnapshot {
  url: string;
  queryParams: Params;
  params: Params;
  context: PageContext;
  cmsRequired: boolean;
  semanticRoute?: string;
}
      

看個例子:

interface myType<T,V>{
  name: T,
  value: V
};

interface jerryType extends myType<string, number>{
  score: number;
}

const a: jerryType = {
  name: 'Jerry',
  value: 1,
  score: 2
};
      

其中 state 的類型,需要定義 RouterReducerState 的擴充類型時傳入:

SAP 電商雲 Spartacus UI 和路由相關的 State 處理

BaseRouterStoreState 類型:隻有一個 url 字段:

SAP 電商雲 Spartacus UI 和路由相關的 State 處理

我們自定義的 ActivatedRouterStateSnapshot,extends 了 BaseRouterStoreState,第一個字段就為 url:

SAP 電商雲 Spartacus UI 和路由相關的 State 處理
type jerryType = {
  name: string
};
interface mySuperType<T extends jerryType>{
  value: T
};

type superJerryType = {
  score: number;
  name: string;
}

let a: mySuperType<superJerryType> = {
  value:{
    score: 1,
    name: 'Jerry'
  }
};

console.log(a);
      

繼續閱讀