如何使用指令+组件+服务制作相同的 ng-template 在 2 ngModules 中工作?
How to make same ng-template using directive+component+service to work in 2 ngModules?
我正在尝试实现一个组件,该组件具有可在多个路由中使用的单个实例。这可以在单个模块中使用一个模板来完成,该模板具有将组件存储在服务中的指令。请参阅 的已接受答案。
我有stackblitz that is copied from here. This works nicely when everything is in same module. However if I place the components in their own module this does not work anymore. See not working example。我试图为服务+组件+指令创建自己的模块,但没有帮助。如何做到这一点?
您可以添加一个 ReusableModule
来导出可重用指令和可重用组件。基本上这就像一个 SharedModule ,然后您可以将其导入到不同的功能模块中。
我在 stackblitz 编辑了您的示例,希望能解决问题:)
编辑:
如果您也需要 ReusableService
的不同实例,您可以通过在 ReusableModule
中设置 providers: [ReusableService]
来确定范围。如果您只需要该服务应用程序的一个实例,则将此服务的提供者保留在 app.module.
中
我正在尝试实现一个组件,该组件具有可在多个路由中使用的单个实例。这可以在单个模块中使用一个模板来完成,该模板具有将组件存储在服务中的指令。请参阅
我有stackblitz that is copied from here. This works nicely when everything is in same module. However if I place the components in their own module this does not work anymore. See not working example。我试图为服务+组件+指令创建自己的模块,但没有帮助。如何做到这一点?
您可以添加一个 ReusableModule
来导出可重用指令和可重用组件。基本上这就像一个 SharedModule ,然后您可以将其导入到不同的功能模块中。
我在 stackblitz 编辑了您的示例,希望能解决问题:)
编辑:
如果您也需要 ReusableService
的不同实例,您可以通过在 ReusableModule
中设置 providers: [ReusableService]
来确定范围。如果您只需要该服务应用程序的一个实例,则将此服务的提供者保留在 app.module.