How to make a component universally accessible in Angular2

前端 未结 2 1671
梦谈多话
梦谈多话 2020-12-09 12:58

I basically want to create a custom dialog component that I can utilize from anywhere in my Angular2 app regardless of where the using component is in the application tree.

相关标签:
2条回答
  • An alternative to using a service is dynamically creating the component using ViewContainerRef/createComponent described here:

    https://www.lucidchart.com/techblog/2016/07/19/building-angular-2-components-on-the-fly-a-dialog-box-example/

    0 讨论(0)
  • 2020-12-09 13:41

    As mentioned in a comment above,

    • Put an Observable inside the service (note, not an EventEmitter)
    • Put a showDialog() API/method on the service that other components can call. The showDialog() method should call next() to send an event.
    • Your dialog component can subscribe to the event and unhide/show itself when it receives an event.

    To wrap an Observable in a service, see this answer.

    0 讨论(0)
提交回复
热议问题