Issue
I have one method in 2 components that does exact same logic. I want DRY code and make a function that does this. How can i make this method as separate function without making a service for it?
private compareWithMainDevice(): void {
this.newBuildingStatisticsService.getMainDeviceAggregates(this.isDetail).subscribe(mainDeviceAggregates => {
this.mainDeviceId = mainDeviceAggregates.device_id;
const mainDev = JSON.parse(JSON.stringify(mainDeviceAggregates));
const other = this.deviceAggregates;
this.deviceAggregates = [mainDev, ...other];
const devs = this.deviceAggregates.reduce((a, b) => {
b.cost.forEach(({ value, timestamp }, i) => {
if (mainDev.cost.length > i && a.cost[i].value > b.cost[i].value) {
return a.cost[i].value -= value;
} else {
return false;
}
});
return a;
});
this.deviceAggregates = [...other, devs];
this.setChartOption(true);
});
}
I can't figure out how to pass the methods of component to this function.
Solution
I'm not sure if I understood your question.
But, in your case, if you want to share compareWithMainDevice method in two components without rewrite twice, you can create an abstract class with this method and your shared class properties (such as mainDeviceId by exemple) into it. Then, each component will extends this class and they will have access to the method.
Answered By - Adrien SAULNIER
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.