I have a desire to improve the organization of my code by moving certain functions into an external file and accessing them from multiple components.
These functions rely on a service:
The MyFunctions Class
import { Service1 } from "../_services";
export class MyFunctions {
constructor(private service: Service1) {}
bookingFunctions = {
confirm: (id: any) => {
this.service.confirmTrip(id);
}
};
}
My Component Implementation
import MyFunctions from "../../../functions";
...
export class AppComponent {
constructor(
private service: Service1
) {}
...
myFunctions = new MyFunctions(this.service);
...
confirmBooking(id: number){
this.myFunctions.bookingFunctions.confirmBooking(id);
}
...
}
After successfully implementing this setup, I am curious as to why it is not more commonly used. Are there any drawbacks to this approach?
Is it acceptable to pass the service instance Service1
to the constructor of the MyFunctions
class?
I find myself wondering, "It works! But how?"