【问题标题】:"...decorated with Injectable" - Angular2“...装饰有可注射” - Angular2
【发布时间】:2016-04-25 15:40:12
【问题描述】:

我正在通过 Youtube 上的视频教程从 Google 学习新框架 Angular2:https://www.youtube.com/watch?v=SEM6-TkuOgo

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

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

异常:错误:未捕获(承诺中):无法解析“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)"} 信息 : “无法解析 '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);

我截取了项目中不同的 TypeScript 文件:http://imgur.com/a/mqb5P

如果有人有一些调试此代码的线索,我会全力以赴! :)

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

【问题讨论】:

    标签: service angular


    【解决方案1】:

    如果你有一个组件ContactListComponent 喜欢

    export class ContactListComponent {
      constructor(private contactService:ContactService) {}
    }
    

    和类似的服务

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

    那么 ContactService 类需要有一个类似@Injectable() 的装饰器

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

    【讨论】:

    • 您好,君特!谢谢,但即使我修复了我的contact.service.ts,与您使用构造函数的建议相比,我也得到了这个:> angular2-polyfills.js:332 错误:TypeError:无法读取未定义的属性'getOptional'(...)所以我已经做了一个 Plunker 来帮助检查代码:plnkr.co/edit/TBeK15tcQlVSkAjRTPSJ
    • plnkr.co/edit/85g0L8?p=preview ngOnInit 有错字。我还将您的代码移到了我的 Plunker 模板中。
    • 哇哦!但即使我纠正了我写 ngOnInit 的错误,我也遇到了一堆错误:i.imgur.com/vTPdftA.png
    • 我猜你的导入有问题,因为代码在我的 Plunker 中运行。
    • 我真的不明白我检查了我所有的 Imports 好几次......哪一个可能是错的?
    【解决方案2】:

    大多数情况下,此类问题是因为构造函数参数中指定的类型的导入是任务或错误。

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

    类似这样的:

    import { ContactService } from './contact.service';
    

    Angular2 负责找出你想要注入构造函数的实例类型。使用 TypeScript,它是使用您指定的参数类型完成的,但需要事先将它们导入到您的模块中。

    依赖注入是使用装饰器完成的:@Injectable 用于简单的类,@Component / @Directive 用于组件/指令。但是你需要一个装饰器。

    【讨论】:

    • 是的,我确认我导入了所有需要的文件,还检查了两次存储库的路径。我制作了一个 Plunker,这样检查代码可能会更容易:plnkr.co/edit/TBeK15tcQlVSkAjRTPSJ
    • 在 index.html 文件中删除 <base href... 是否也有同样的问题?我让它与这个 plunkr 一起工作:plnkr.co/edit/ZiLSZinJdT4VCoOT90AW?p=preview.
    • 不管有没有<base href="/">,都没有变化。
    猜你喜欢
    • 1970-01-01
    • 2016-01-28
    • 2016-06-09
    • 2016-02-02
    • 2016-03-23
    • 2016-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多