@Injectable()和@Inject中的差异是什么

时间:2016-06-21 09:04:47

标签: angular inject injectable

我正在开发angular2我已经创建了服务并使用组件中的@Inject注入了这些服务。我对服务本身使用@Injectable()以及它所带来的差异感到困惑。

1 个答案:

答案 0 :(得分:4)

@Inject()是一种让Angular 2知道需要注入参数的手动机制

@Injectable()让Angular 2知道一个类可以与依赖注入器一起使用。 @Injectable() is not strictly required如果该类上有其他Angular 2装饰器。重要的是任何将要注入Angular 2的类都会被装饰。

然而,最佳做法是使用@Injectable()装饰注射剂,因为它对读者来说更有意义。