问题描述
我正在尝试将ViewContainerRef
注入服务,但收到错误No provider for ViewContainerRef!
.我发现了此 PR,它完全满足了我的需求,而且似乎具有被合并.
I'm attempting to inject ViewContainerRef
into a service, but am receiving error No provider for ViewContainerRef!
. I found this PR, which addresses exactly what I'm after and it appears to have been merged.
请注意,我知道如何通过使用占位符使用此.
Note that I am aware of how to achieve this by using a placeholder using this.
推荐答案
服务对任何视图都是完全不可知的.如果要使服务与特定视图关联,则应将其添加到特定父组件/指令内的提供程序列表中,并将ViewContainerRef作为参数传递给服务的方法之一.
Services are meant to be completely agnostic to any view. If you are trying to have a service associated with a particular view, then you should add it to the providers list within a specific parent component/directive and pass the ViewContainerRef as an argument into one of the service's methods.
这篇关于如何将ViewContainerRef注入服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!