组件是可注射的? [英] Component as Injectable?

查看:19
本文介绍了组件是可注射的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建自己的 Modal 组件,以便能够在我的 Angular2 应用程序中重复使用.我正在考虑不同的方法,我想知道是否可以创建也用作 @Injectable@Component?我正在考虑这个,因为我想为 Modal 构建一个模板,并将其保存在一个地方.

I am trying to build my own Modal component, that I would be able to reuse throughout my Angular2 Apps. I am considering different approaches, and I am wondering if it is possible to create @Component that also serves as @Injectable? I am considering this, as I want to build a template for the Modal, and keep it in one place.

谢谢

推荐答案

一个组件默认是可注入的 @Component()(或 @Directive())包括 <代码>@Injectable().

A component is injectable by default @Component() (or @Directive()) includes @Injectable().

不要期望获得注入的组件的特定实例.例如,这适用于 constructor(@Host() private parentComponent),其中 DI 查找仅限于父注入器直到宿主元素的注入器.

Don't expect to get a specific instance of a component injected. This works for example with constructor(@Host() private parentComponent) where DI lookup is limited to parent injectors up to the injector of the host element.

通常用于您的用例 DynamicComponentLoader 仅接受组件的类型,而不是实例.依赖注入只会创建组件类的实例,而不是例如关联的视图.

Normally for your use case DynamicComponentLoader is used which only accepts the type of a component, not an instance. Dependency injection would just create an instance of the components class but not for example the associated view.

这篇关于组件是可注射的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆