UserService
我有一个组件在专用模块中呈现用户列表(它注入其相关的服务称为)
ProductService
我有另一个组件在专用模块中呈现产品列表(注入其相关服务)
在前面列表的每一行中,我有另一个组件,它呈现一个带有下拉菜单的上下文按钮,显示操作列表(编辑、删除等...)(这是两个列表之间的共享组件)
从共享组件中,我想根据呈现的列表调用相关的服务方法(UserService.method()、ProductService.method()),我该怎么做?如何在特定上下文中注入我需要的相关服务?我可以抽象服务以注入共享组件吗?
我想避免将所有服务注入共享组件。
我有点失落。我希望我的解释很清楚。谢谢
这里是伪代码
/*********** USER MODULE ***********/
@Injectable({
providedIn: 'root',
})
export class UserService{
dosomething() {}
}
@Component(...)
export class UserListComponent implements OnInit {
users: Users = [];
constructor(private readonly userService: UserService) {}
ngOnInit(): void {
this.users = this.userService.users;
}
}
/*********** PRODUCT MODULE ***********/
@Injectable({
providedIn: 'root',
})
export class ProductService{
dosomething() {}
}
@Component(...)
export class ProductListComponent implements OnInit {
products: Products = [];
constructor(private readonly productService: ProductService) {}
ngOnInit(): void {
this.products = this.productService.product;
}
}
/*********** CORE MODULE ***********/
@Component(...)
export class DropdownActionButton {
constructor(
private readonly userService: UserService,
private readonly productService: ProductService,
// ... I have to add a new service as dependency if a new list is created ...
) {}
handleClick(){
if('you are in the user list') {
userService.dosomething()
}
if('you are in the product list') {
productService.dosomething()
}
// ... I have to do a new if if a new list is create ....
}
}