为什么Http服务需要@Injectable decorator而定制服务不需要Angular2
为什么我在使用为什么Http服务需要@Injectable decorator而定制服务不需要Angular2,angular,typescript,dependency-injection,Angular,Typescript,Dependency Injection,为什么我在使用Http进行DI时需要包含@Injectable,但在使用我创建的自定义服务进行DI时不需要它 比如说 //no @Injectable() here export class Test { constructor(private customService: CustomService){} } /*-------------*/ @Injectable() // <== required here export class HttpTest { construct
Http
进行DI时需要包含@Injectable
,但在使用我创建的自定义服务进行DI时不需要它
比如说
//no @Injectable() here
export class Test {
constructor(private customService: CustomService){}
}
/*-------------*/
@Injectable() // <== required here
export class HttpTest {
constructor(private http: Http){}
}
//此处没有@Injectable()
导出类测试{
构造函数(私有customService:customService){}
}
/*-------------*/
@如果构造函数中有需要注入的参数,则需要Injectable()//@Injectable()
。看
@Injectable()
将类标记为可供注入器使用
实例化。一般来说,喷油器在以下情况下会报告错误:
尝试实例化未标记为@Injectable()
的类
碰巧,您可以从第一个选项中省略@Injectable()
HeroService的版本,因为它没有注入的参数。但是你
现在必须拥有它,因为服务具有注入依赖项。你需要
这是因为Angular需要构造函数参数元数据才能
插入记录器
您的示例不起作用,因为它需要在测试
类上使用@Injectable()
,它起作用了吗?请创建plunker。这对我不起作用<代码>无法解析测试的所有参数:(?)
请显示如何使用测试。该语句不正确,当用作服务时,它肯定会抛出injector错误。