为什么 SAP 电商云 Spartacus UI RouterModule.forChild 传入的 path 为 null
RouterModule.forChild 傳入的 path 為 null:
注意,RouterModule.forRoot 只能調用一次。RouterModule 有一個 forChild 方法,也可以傳入 Route 對象數組,然而盡管 forChild 和 forRoot 方法都包含路由指令和配置,但是 forRoot 可以返回 Router 對象,由于 Router 服務會改變 瀏覽器 location 對象,而 location 對象又是一個全局單例對象,所以 Router 服務對象也必須全局單例。
必須在根模塊中只使用一次 forRoot 方法,feature Module中應當使用 forChild 方法。
當匹配到路由路徑時,路由狀態 component 屬性定義的組件會被渲染在 router-outlet 指令 掛載的地方,即渲染激活組件的動態元素。被渲染的組件會作為 router-outlet 元素的兄弟節點而不是子節點,router-outlet 元素也可以層層嵌套,形成父子路由關系。
回到本文圖1,path 為 null,看到 Route 里的定義:path 不能和 custom matcher 混用。
但是標準的 router 定義里,matcher 后面不是復數形式:
在 routes-config.ts 里看到的 matchers 定義:
類型是 UrlMatcher 的數組,而 UrlMatcher 是 Angular 標準類型:
https://angular.io/api/router/Route#path
這個函數的定義:
type UrlMatcher = (segments: UrlSegment[], group: UrlSegmentGroup, route: Route) => UrlMatchResult | null;接受下列三個參數:
- segment : URL 段的數組。
- group : 一個段組。
- route :要匹配的路由。
返回類型為 UrlMatchResult.
看一個例子:
export function htmlFiles(url: UrlSegment[]) {return url.length === 1 && url[0].path.endsWith('.html') ? ({consumed: url}) : null; }export const routes = [{ matcher: htmlFiles, component: AnyComponent }];這幾個文件都相關:
所以 SAP Spartacus 必定有一個實現,把多個 matchers 合并為一個單一的 matcher,只有后者才能被 Angular 路由框架正確識別。
做個測試:
最后的效果:
還是能看到 header 和 footer 區域。
還是位于 main 區域:
因為 router-outlet 的父節點還是位于 storefront.component.html 之內:
更多Jerry的原創文章,盡在:“汪子熙”:
總結
以上是生活随笔為你收集整理的为什么 SAP 电商云 Spartacus UI RouterModule.forChild 传入的 path 为 null的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 中山大学软件所软件工程(中山大学软件研究
- 下一篇: SAP Spartacus UI Con