TL;DR:
We want to create a "global"
Detailed:
We are building an Angular 2 app and some of our components need to register with a global service on
The two approaches we considered were implementing the logic in every component or creating a common base class. However, these approaches have limitations. In the first approach, we would have to repeat the same logic in every component. In the second approach, all classes would have to extend the same base class, which is not ideal if they also need to extend other base classes.
Our proposed solution is to use an interface that can be implemented by all necessary components. We will then register a global function that checks if the component implements the interface and calls the appropriate function to register/unregister it. This approach avoids the limitations of the other two approaches and provides a more explicit implementation.
Our questions are: Can Angular 2 support this? Is this approach incorrect? Are there any better ideas?
We want to create a "global"
ngOnInit
and ngOnDestroy
function for our Angular 2 app. We need components to register and unregister with a global service, but we don't want to implement this logic in every component or a common base class. Instead, we propose using an interface that can be implemented by all necessary components. Then, we will register a global function which checks if the component implements the interface and calls the appropriate function to register/unregister it.Detailed:
We are building an Angular 2 app and some of our components need to register with a global service on
ngOnInit
and unregister on ngOnDestroy
. We don't want to implement this logic in every component, nor do we want to create a common base class that all classes must extend. Instead, we propose using an interface that can be implemented by all necessary components. Then, we will register a global function which checks if the component implements the interface and calls the appropriate function to register/unregister it.The two approaches we considered were implementing the logic in every component or creating a common base class. However, these approaches have limitations. In the first approach, we would have to repeat the same logic in every component. In the second approach, all classes would have to extend the same base class, which is not ideal if they also need to extend other base classes.
Our proposed solution is to use an interface that can be implemented by all necessary components. We will then register a global function that checks if the component implements the interface and calls the appropriate function to register/unregister it. This approach avoids the limitations of the other two approaches and provides a more explicit implementation.
Our questions are: Can Angular 2 support this? Is this approach incorrect? Are there any better ideas?