相同的方法,不同的模板:如何将方法保留在一处?

问题描述 投票:0回答:1

该项目现在有不同的桌面和移动应用程序布局组件。 但两个组件内部实际上使用相同的逻辑。这是简单的操作,如重命名/删除/创建。

如果我们需要更改某些内容,那么工作量总是会增加一倍,因为您需要更改两个组件中的逻辑。

如何让两个组件使用相同的方法,但保留不同的模板? 最佳做法是什么?

angular architecture
1个回答
0
投票
Consider you are having 3 components which will have same functionality.
Lets say , Component 1 = one , Component 2 = two, component 3 = three.

Step 1 :-
Make a service file name commonService.ts file.
Make a function :-
 public getData() {
 //do your code here
 }
 
Step 2:-
Register commonService.ts file in contructor of  all 3 components as follows:-
contructor(public commonService: CommonService){}

call getData() function in any component you want:-
this.commonService.getData();
© www.soinside.com 2019 - 2024. All rights reserved.