天天看點

SAP Spartacus的自定義Angular Url Matcher實作

https://microlearning.opensap.com/media/Custom+Angular+URL+Matchers+in+Spartacus+Storefront±+SAP+Commerce+Cloud/1_hhjqkiuy/178316081

Angular, and Spartacus by extension, allows you to configure string patterns to match routes against URLs. An example is /product/:productCode, which has two segments. The first segment, product, is a static segment that determines the URL is a product page type, and the second segment, :productCode, is a dynamic parameter.

上面提到的url,有兩個segments,其中第一個product segment,為靜态segment,說明該url代表一個product page type,而第二個segment :productCode, 是一個動态參數。

有時存在需求,需要在一個segment内同時實作靜态和動态兩種segment:

However, there may be cases where you need to work with URL segments that contain both static and dynamic parts within a single segment. An example is /macbook-p, where mackbook is a dynamic product code, and -p is a static part that determines the URL is a product page type. In this case, you need to implement a custom Angular UrlMatcher.

這種需求通過UrlMatcher來滿足。

源代碼:

SAP Spartacus的自定義Angular Url Matcher實作

将自定義實作的matcher配置到module裡:

SAP Spartacus的自定義Angular Url Matcher實作

上面代碼中paths裡傳入的:customProductCode是一個新的屬性,需要通過normalizer添加進去:

SAP Spartacus的自定義Angular Url Matcher實作

最後将custom normalizer注入到app module中:

SAP Spartacus的自定義Angular Url Matcher實作

繼續閱讀