投影策略
ProjectionStrategy 是由 @abp/ng.core 包提供的抽象类。有三个投影策略继承自该类:ComponentProjectionStrategy、RootComponentProjectionStrategy 和 TemplateProjectionStrategy。这些策略实现了相同的方法和属性,均可帮助您定义内容投影的工作方式。
ComponentProjectionStrategy
ComponentProjectionStrategy 是继承自 ProjectionStrategy 的类,允许您将组件投影到容器中。
构造函数
constructor(
component: T,
private containerStrategy: ContainerStrategy,
private contextStrategy?: ContextStrategy,
)
component是您要投影的组件类containerStrategy是投影组件时将使用的ContainerStrategycontextStrategy是将在投影组件上使用的ContextStrategy(默认值:无)
请参阅 ContainerStrategy 和 ContextStrategy 文档了解其用法。
injectContent
injectContent(injector: Injector): ComponentRef<T>
此方法准备容器、解析组件、设置其上下文并将其投影到容器中。它返回一个 ComponentRef 实例,您应保留该实例以便稍后清除投影的组件。
RootComponentProjectionStrategy
RootComponentProjectionStrategy 是继承自 ProjectionStrategy 的类,允许您将组件投影到文档中,例如将其附加到 <body>。
构造函数
constructor(
component: T,
private contextStrategy?: ContextStrategy,
private domStrategy?: DomStrategy,
)
component是您要投影的组件类contextStrategy是将在投影组件上使用的ContextStrategy(默认值:无)domStrategy是插入组件时将使用的DomStrategy(默认值:AppendToBody)
请参阅 ContextStrategy 和 DomStrategy 文档了解其用法。
injectContent
injectContent(injector: Injector): ComponentRef<T>
此方法解析组件、设置其上下文并将其投影到文档中。它返回一个 ComponentRef 实例,您应保留该实例以便稍后清除投影的组件。
TemplateProjectionStrategy
TemplateProjectionStrategy 是继承自 ProjectionStrategy 的类,允许您将模板投影到容器中。
构造函数
constructor(
template: T,
private containerStrategy: ContainerStrategy,
private contextStrategy?: ContextStrategy,
)
template是您要投影的TemplateRefcontainerStrategy是投影组件时将使用的ContainerStrategycontextStrategy是将在投影组件上使用的ContextStrategy(默认值:无)
请参阅 ContainerStrategy 和 ContextStrategy 文档了解其用法。
injectContent
injectContent(): EmbeddedViewRef<T>
此方法准备容器,并将模板与定义的上下文一起投影到容器中。它返回一个 EmbeddedViewRef,您应保留该实例以便稍后清除投影的模板。
预定义投影策略
预定义的投影策略可通过 PROJECTION_STRATEGY 常量访问。
AppendComponentToBody
PROJECTION_STRATEGY.AppendComponentToBody(
component: T,
contextStrategy?: ComponentContextStrategy<T>,
)
将给定上下文设置到组件,并将其放置在文档中 <body> 标签的末尾。
AppendComponentToContainer
PROJECTION_STRATEGY.AppendComponentToContainer(
component: T,
containerRef: ViewContainerRef,
contextStrategy?: ComponentContextStrategy<T>,
)
将给定上下文设置到组件,并将其放置在容器的末尾。
AppendTemplateToContainer
PROJECTION_STRATEGY.AppendTemplateToContainer(
templateRef: T,
containerRef: ViewContainerRef,
contextStrategy?: ComponentContextStrategy<T>,
)
将给定上下文设置到模板,并将其放置在容器的末尾。
PrependComponentToContainer
PROJECTION_STRATEGY.PrependComponentToContainer(
component: T,
containerRef: ViewContainerRef,
contextStrategy?: ComponentContextStrategy<T>,
)
将给定上下文设置到组件,并将其放置在容器的开头。
PrependTemplateToContainer
PROJECTION_STRATEGY.PrependTemplateToContainer(
templateRef: T,
containerRef: ViewContainerRef,
contextStrategy?: ComponentContextStrategy<T>,
)
将给定上下文设置到模板,并将其放置在容器的开头。
ProjectComponentToContainer
PROJECTION_STRATEGY.ProjectComponentToContainer(
component: T,
containerRef: ViewContainerRef,
contextStrategy?: ComponentContextStrategy<T>,
)
清空容器,将给定上下文设置到组件,并将其放置在已清空的容器中。
ProjectTemplateToContainer
PROJECTION_STRATEGY.ProjectTemplateToContainer(
templateRef: T,
containerRef: ViewContainerRef,
contextStrategy?: ComponentContextStrategy<T>,
)
清空容器,将给定上下文设置到模板,并将其放置在已清空的容器中。
抠丁客


