SAP Spartacus Reference App Structure

https://sap.github.io/spartac...git

該概念在 SAP Spartacus 3.1 版本引入。github

這個 Reference App Structure 是 SAP 推薦客戶在搭建本身的 Spartacus Storefront 時所用的參考。app

Spartacus 包含若干能夠 lazy load 的 feature libraries.ide

Customizations and third-party code add further complexity, and you can end up with modules that are difficult to maintain because they mix too many of these elements together.

定製化和第三方代碼進一步增長了項目複雜度,若是處理不得當,很容易出現堆砌了大量 module,難於維護的狀況。ui

This can be solved by defining and adhering to a standardized structure, such as the Spartacus reference app structure.

而遵循 Spartacus reference app structure 來開發,就能避免此類問題出現。this

Having a standardized structure also makes it easier to onboard new developers to your project, to handle external support cases, and to take care of audits.

有一套標準的工程結構,也便於新開發人員進入項目後,快速熟悉項目並上手。spa

結構

1. AppModule

2. SpartacusModule

3.BaseStorefrontModule

從 @spartacus/storefront 裏導入,再導出。code

4.SpartacusFeatureModule

5. feature related modules

6. 配置 module

Every Angular application has a root app module, usually named AppModule. In the reference app strcuture, this module includes application-wide imports, and avoids complex module imports related to Spartacus by handling only one SpartacusModule.

每一個 Angular 應用都有一個 root app module,一般命名爲 AppModule. 在 SAP Spartacus reference app structure 裏,AppModule 包含了應用層級的 imports:ci

Both Angular Router and NgRx are used by Spartacus, but these affect the global application, so they are kept outside of the SpartacusModule and are imported directly in the AppModule.

從上圖可見,StoreModule 和 EffectsModule 等 module,由於整個應用的其餘定製開發極可能也會用到,所以直接在 AppModule 裏 import,而沒有放到 SpartacusModule 裏。element

SpartacusModule 包含三部分:

  1. The BaseStorefrontModule, which encapsulates core Spartacus imports that are usually required by most Spartacus applications.

包含了任何 Spartacus 應用都必需的 imports.

The BaseStorefrontModule is imported directly from @spartacus/storefront.
  1. The SpartacusFeaturesModule, which encapsulates Spartacus features.

Spartacus feature 又分爲兩部分:來自 @Spartacus/storefront 和 features 文件夾下的 modules.

  1. The SpartacusConfigurationModule, which encapsulates the general Spartacus configuration.

配置信息:

Feature-specific configurations can be kept either in feature modules, or in the SpartacusConfigurationModule. Keeping them in feature modules helps to maintain a good separation of concerns, so it is generally recommended.

feature 相關的配置信息,推薦放到 feature modules 裏,從而遵循關注點分離的原則。

更多Jerry的原創文章,盡在:"汪子熙":