@Injectable()和@Inject中的差异是什么 [英] what is diff in @Injectable() and @Inject

查看:99
本文介绍了@Injectable()和@Inject中的差异是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用angular2,我已经创建了服务,并使用@Inject在组件中注入了这些服务.我对在服务本身中使用@Injectable()及其造成的差异感到困惑.

I am working on angular2 I have created service and inject those service using @Inject in component. I am confuse with the use of @Injectable() in service itself and what diff it makes.

推荐答案

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

@Inject() is a manual mechanism for letting Angular 2 know that a parameter needs to be injected

@Injectable()让Angular 2知道一个类可以与依赖项注入器一起使用.如果该类具有其他Angular 2装饰器,则 @Injectable()并非严格要求.重要的是要装饰将要用Angular 2注入的任何类.

@Injectable() lets Angular 2 know that a class can be used with the dependency injector. @Injectable() is not strictly required if the class has other Angular 2 decorators on it. What is important is that any class that is going to be injected with Angular 2 is decorated.

但是,最佳实践是使用@Injectable()装饰可注入对象,因为这对读者来说更有意义.

However best practice is to decorate injectables with @Injectable(), as it is makes more sense to the reader.

这篇关于@Injectable()和@Inject中的差异是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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