天天看点

如何通过代码获得当前SAP Spartacus Component渲染所基于的slot名称

什么是PageSlotComponent

如何通过代码获得当前SAP Spartacus Component渲染所基于的slot名称

解决方案

如何通过代码获得当前SAP Spartacus Component渲染所基于的slot名称

理论依据

In Angular you can inject the closest parent component of a given type, by passing it as a constructor dependency.

You might want to add @Optional() decorator if you plan to use your component outside the content slot (i.e. as a static component). It will help avoid crashing app in the runtime, but will just resolve PageSlotComponent instance safely to null.

看一个运行时的例子:

如何通过代码获得当前SAP Spartacus Component渲染所基于的slot名称

Angular里可以很容易的通过依赖注入,在一个Component的构造函数里,注入其祖先引用作为依赖。这样,Component里就能访问其祖先实例上的数据。

继续阅读