小编A B*_*ife的帖子

Angular - 通过@Input 属性进行依赖注入

@Input属性中注入“服务”依赖项是一个好习惯吗?此上下文中的服务不是在根级别管理的单例实例,而是接口不同实现的多个实例。

考虑以下示例:在 Angular 库中,ShapeComponent 依赖于 ShapeService(接口)。

成分

@Component({
  selector: 'ex-shape',
  templateUrl: '..',
})    

export class ShapeComponent {
   constructor(shapeServiceCtor: ShapeService)
    
   @Input shapeServiceInput: ShapeService;
}
Run Code Online (Sandbox Code Playgroud)

解决依赖关系的一种简单方法是设置输入属性,如下面的代码所示。

<ex-shape [shapeServiceInput]="rectangleShapeService" />
<ex-shape [shapeServiceInput]="ellipseShapeService" />
<ex-shape [shapeServiceInput]="polygonShapeService" />
Run Code Online (Sandbox Code Playgroud)

上述方法是否适用于解决组件中的依赖关系?

如果使用输入属性方法,则服务/依赖项必须以相同的方式传播到子组件。这种方法的缺点是父组件必须接受所有依赖项作为输入属性。

是否有任何推荐的方法可以在库级别注入和作用域依赖项?

dependency-injection typescript angular angular-dependency-injection

7
推荐指数
1
解决办法
1001
查看次数

Action中的泛型类型?

Action构造是通用的,但它可以支持泛型类型吗?

下面显示的代码片段就是我想要实现的.

我知道还有其他方法可以做到,但我很好奇它是否可以在Action构造中实现.

void SomeMethod()
{
    Action<int> Initialize = //<T> and where T is all that stuff
    (index) =>
    {
        T obj = new T();
        obj.Initialize(index);
        obj.DoWork();
    };

    Initialize<TypeA>(1);
    Initialize<TypeB>(2);
}
Run Code Online (Sandbox Code Playgroud)

c#

2
推荐指数
1
解决办法
84
查看次数