SAP Spartacus PageLayoutComponen
我们以前介绍过,经过 SAP Spartacus 路由配置后的 routes 数组,其 route 数据结构的 Component 属性,清一色指向 generic 的 PageLayoutComponent,那么后者怎么知道自己应该加载哪些 Angular Component 的数据呢?
![](https://img.haomeiwen.com/i2085791/f3f2c46838d6f6f0.png)
我们首先看看是哪些 template 调用了 PageLayoutComponent 的 selector:
![](https://img.haomeiwen.com/i2085791/de78ddb9cb7a2b8c.png)
只有三处:
![](https://img.haomeiwen.com/i2085791/b4ac54f576e7c1ac.png)
运行时却有四处,这不奇怪,因为 product 明细页面是通过路由加载的,故出现在 router-outlet 下面。
![](https://img.haomeiwen.com/i2085791/00da018740aaedc3.png)
在其构造函数里加打印语句是无济于事的,因为这个 Component 的所有字段都是 Observable 类型,得 subscribe 了才能看到。
![](https://img.haomeiwen.com/i2085791/fef40826a74ee760.png)
所以我在其 @input 字段上设置了一个断点:
![](https://img.haomeiwen.com/i2085791/4b8c3ef9b67ea9b1.png)
![](https://img.haomeiwen.com/i2085791/5d245eaac294f5e6.png)
渲染触发点是第2行的 cxOutlet,触发第10行的 cx-page-layout Component 的创建:
![](https://img.haomeiwen.com/i2085791/9a3e4d4b3b24da5e.png)
Component 实例(即 PageLayoutComponent)创建完毕后,给其 input 属性赋值:
![](https://img.haomeiwen.com/i2085791/b899d9b296623363.png)
上图的赋值,会触发下图第14行代码执行。
![](https://img.haomeiwen.com/i2085791/661c6b06ff18e238.png)
section$ 是一个 subject,其 next 逻辑是遍历其 listener,逐一通知:
![](https://img.haomeiwen.com/i2085791/e1a2e66f4c1adf52.png)
在模板文件 async pipe 执行时,触发 Observable 的执行:
![](https://img.haomeiwen.com/i2085791/1ecff4bb0b5506f3.png)
![](https://img.haomeiwen.com/i2085791/03a76dfbdb3e4607.png)
根据 page, section 和 breakpoint,拿到该 section 里的 slots:
![](https://img.haomeiwen.com/i2085791/433089000dddaea1.png)
layoutslot 的 key 是 page template:
![](https://img.haomeiwen.com/i2085791/d56787e354965aed.png)
![](https://img.haomeiwen.com/i2085791/cc5be95278ccf31b.png)
这个 page template 里并没有一个 section 名叫 header:
![](https://img.haomeiwen.com/i2085791/38ffcccc6ce215ff.png)
所以我们就 fallback 到全局配置里去取 header 的 slot 配置:
![](https://img.haomeiwen.com/i2085791/2cadedbcf7770f7e.png)
getResponsiveSlotConfig:
![](https://img.haomeiwen.com/i2085791/baf8491d04ad1205.png)
先看 layoutConfig 里有没有专门为 xl 而设置的配置,发现没有,只有 lg 的:
![](https://img.haomeiwen.com/i2085791/e9c9f43872085bfe.png)
就找和 xl 最接近的,答案是 lg:
![](https://img.haomeiwen.com/i2085791/79dee63762258f6b.png)
现在就知道了:LandingPage2Template 模板的 header 区域,应该显示下列这些 slots:
![](https://img.haomeiwen.com/i2085791/09d7c00f386d2d04.png)
更多Jerry的原创文章,尽在:"汪子熙":
![](https://img.haomeiwen.com/i2085791/97c4752c0e619c44.png)