[英]ProvidedIn Root Services within a Demo App
I am developing an Angular6 library to be used as the basis of many internal websites.我正在开发一个 Angular6 库,用作许多内部网站的基础。 It will be providing mostly services, but also provide a few components.
它将主要提供服务,但也提供一些组件。
I want to create a Demo App for this library that can demonstrate various ways of using the services, in the same way that a Component library would demonstrate the various ways of using the components.我想为这个库创建一个演示应用程序,它可以演示使用服务的各种方式,就像组件库演示使用组件的各种方式一样。
The problem is that these services were designed to be providedIn root, making them singletons at root.问题是这些服务被设计为在根目录中提供,使它们成为根目录中的单例。 This makes it hard to have a Demo App that can show different service behaviors in different routes, without hard-restarting the application in the browser.
这使得很难有一个 Demo App 可以在不同的路由中显示不同的服务行为,而无需在浏览器中硬重启应用程序。
Is there a strategy I can use to make my library act normally when used within an Application (app.module) but be able to constructed/destructed within a Demo App?在应用程序(app.module)中使用时,我可以使用一种策略使我的库正常运行,但能够在演示应用程序中构造/销毁?
If I create a LibraryRootModule and then set everything to be providedIn LibraryRootModule, would that help?如果我创建一个 LibraryRootModule,然后在 LibraryRootModule 中设置要提供的所有内容,会有帮助吗? How would AOT work with that?
AOT 将如何处理它? I'm also concerned that third-party dependencies that use providedIn root may not work well alongside this strategy.
我还担心使用providedIn root 的第三方依赖项可能无法与此策略一起很好地工作。
Any insights would be appreciated.任何见解将不胜感激。 Thanks.
谢谢。
1) May be you should use forRoot() pattern: https://angular.io/guide/singleton-services#the-forroot-pattern ; 1)也许你应该使用 forRoot() 模式: https://angular.io/guide/singleton-services#the-forroot-pattern ;
2) May be you should use different Angular app on the one website. 2) 可能你应该在一个网站上使用不同的 Angular 应用程序。 And use Platform Injector: https://blog.angularindepth.com/angular-dependency-injection-and-tree-shakeable-tokens-4588a8f70d5d
并使用平台注入器: https://blog.angularindepth.com/angular-dependency-injection-and-tree-shakeable-tokens-4588a8f70d5d
Hope it's help!希望有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.