Service &引用;。。。用“可注射”装饰角2

Service &引用;。。。用“可注射”装饰角2,service,angular,Service,Angular,我正在通过Youtube上的视频教程从谷歌学习新框架Angular2: 当我开始了解服务时,我遇到了不同类型的错误,例如: 异常:无法解析“ContactListComponent”(?)的所有参数。确保所有参数都用Inject修饰或具有有效的类型批注,并且“ContactListComponent”用Injectable修饰 异常:错误:未捕获(承诺中):无法解析“ContactListComponent”(?)的所有参数。确保所有参数都用Inject修饰或具有有效的类型批注,并且“Conta

我正在通过Youtube上的视频教程从谷歌学习新框架Angular2:

当我开始了解服务时,我遇到了不同类型的错误,例如:

异常:无法解析“ContactListComponent”(?)的所有参数。确保所有参数都用Inject修饰或具有有效的类型批注,并且“ContactListComponent”用Injectable修饰

异常:错误:未捕获(承诺中):无法解析“ContactListComponent”(?)的所有参数。确保所有参数都用Inject修饰或具有有效的类型批注,并且“ContactListComponent”用Injectable修饰

angular2 polyfills.js:469未处理的承诺拒绝:无法解析“ContactListComponent”(?)的所有参数。确保所有参数都用Inject修饰或具有有效的类型批注,并且“ContactListComponent”用Injectable修饰;区域:角度;任务:承诺;价值:

NoAnnotationError{消息:“无法解析'ContactListComp…ntactListComponent'的所有参数是用Injectable修饰的。”,堆栈:“错误:无法解析'ContactL…node_modules/angular2/bundles/angular2.js:477:94'的所有参数”} 消息 : “无法解析'ContactListComponent'(?)的所有参数。请确保所有参数都用Inject修饰或具有有效的类型批注,并且'ContactListComponent'用Injectable修饰。” 堆栈 : 错误:无法解析“ContactListComponent”(?)的所有参数。请确保所有参数都用Inject修饰或具有有效的类型批注,并且“ContactListComponent”已修饰/

angular2 polyfills.js:471错误:未捕获(承诺中):无法解析“ContactListComponent”(?)的所有参数。请确保所有参数都用Inject修饰或具有有效的类型批注,并且“ContactListComponent”用Injectable修饰。(…)

angular2 polyfills.js:469未处理的承诺拒绝:无法解析“ContactListComponent”(?)的所有参数。请确保所有参数都用Inject修饰或具有有效的类型批注,并且“ContactListComponent”用Injectable修饰。;区域:;任务:Promise.then;值:NoAnnotationError{消息:无法解析“ContactListComp…ntactListComponent”的所有参数是用Injectable修饰的。”,堆栈:“错误:无法解析“ContactL…node_modules/angular2/bundles/angular2.js:477:94”的所有参数

angular2 polyfills.js:471错误:未捕获(承诺中):无法解析“ContactListComponent”(?)的所有参数。请确保所有参数都用Inject修饰或具有有效的类型批注,并且“ContactListComponent”用Injectable修饰。(…)

我不理解控制台的真正含义,我试图注释代码的某些部分以测试代码是否编译

如果您觉得问题可能出在contact-list.component.ts中的此部分:

constructor(private _contactService: ContactService) {}
这一个也在contact.service.ts中:

return Promise.resolve(CONTACTS);
我拍摄了项目中不同类型脚本文件的屏幕截图:

如果有人有一些线索来调试这段代码,我洗耳恭听


PS:如果可能有帮助的话,我可以从截图中复制/粘贴代码。

大多数情况下,这样的问题是因为构造函数参数中指定的类型的导入不正确

是否在component.list.ts文件中导入“ContactService”类型

大概是这样的:

import { ContactService } from './contact.service';
Angular2负责找出要注入构造函数的实例类型。使用TypeScript,可以使用指定的参数类型来完成,但它们需要在之前导入到模块中


依赖项注入是使用decorator完成的:对于简单类使用@Injectable,对于组件/指令使用@Component/@指令。但是,如果您有组件
ContactListComponent
之类的组件,则需要一个decorator。

export class ContactListComponent {
  constructor(private contactService:ContactService) {}
}
还有像这样的服务

export class ContactService {
  constructor(private someDep:SomeDependency) {}
}
然后ContactService类需要有一个类似的
@Injectable()
装饰器

@Injectable()
export class ContactService {
  constructor(private someDep:SomeDependency) {}
}

是的,我验证了我导入了所有需要的文件,还检查了两次存储库的路径。我制作了一个Plunker,这样可以更容易地检查代码:通过删除
无更改(有或没有
)是否存在同样的问题。嗨,居里!谢谢!但即使我修复了contact.service.ts,与您的建议相比,使用我得到的构造函数:>angular2 polyfills.js:332错误:TypeError:无法读取未定义(…)的属性“getOptional”因此,我制作了一个Plunker来帮助检查代码:
ngOnInit
有一个输入错误。我还将您的代码移动到了我的Plunker模板中。哇,没错!但即使我纠正了我写ngOnInit的错误,我也犯了一系列错误:我猜您的导入有问题,因为代码在我的Plunker中工作。我真的不知道我知道我已经检查了好几次所有的进口商品了……哪一个可能是错的?