Angular 2 - Injectable() 装饰器
Angular 2 - Injectable() decorator
我不完全理解Angular中的@Injectable
装饰器 2.即使我不使用它,服务也能正常工作。 (看下面的例子)
使用 @Injectable
装饰器的目的是什么?
下面是两个例子,一个有一个没有 Injectable
以及各自的 plunker 例子:
使用@Injectable Example1`
@Injectable()
export class AppService{
get():any[]{
return "something"
}
没有@Injectable Example2 </code></p>
<pre><code>export class AppService{
get():any[]{
return "something"
}
我当然知道使用 @Injectable
装饰器的原因,用于依赖注入,但正如我之前所说,我不完全理解。有人可以解释一下吗?
如果服务没有构造函数参数,则不需要 Injectable。无论如何都考虑将它添加到所有服务中,因为这是一个常见的错误,即添加了后面的参数但忘记了 @Injectable()
装饰器,因为它之前正在工作。
我不完全理解Angular中的@Injectable
装饰器 2.即使我不使用它,服务也能正常工作。 (看下面的例子)
使用 @Injectable
装饰器的目的是什么?
下面是两个例子,一个有一个没有 Injectable
以及各自的 plunker 例子:
使用@Injectable Example1`
@Injectable()
export class AppService{
get():any[]{
return "something"
}
没有@Injectable Example2 </code></p>
<pre><code>export class AppService{
get():any[]{
return "something"
}
我当然知道使用 @Injectable
装饰器的原因,用于依赖注入,但正如我之前所说,我不完全理解。有人可以解释一下吗?
如果服务没有构造函数参数,则不需要 Injectable。无论如何都考虑将它添加到所有服务中,因为这是一个常见的错误,即添加了后面的参数但忘记了 @Injectable()
装饰器,因为它之前正在工作。