Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么Http服务需要@Injectable decorator而定制服务不需要Angular2_Angular_Typescript_Dependency Injection - Fatal编程技术网

为什么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错误。