angular - 注入(inject)剂在 angular 2.0 中不起作用

标签 angular typescript1.5

我最近开始玩 Angular2。我已经尝试让注入(inject)剂发挥作用大约半天了,但我仍然无法弄清楚我做错了什么。

为了尽可能简单,我从 5 Min Quickstart 复制了代码在官方网页。演示本身工作正常,但是当我尝试使用注入(inject)剂时,我收到一条错误消息

ORIGINAL ERROR: Cannot resolve all parameters for MyAppComponent. Make sure they all have valid type or annotations.

我的 typescript 文件

/// <reference path="typings/angular2/angular2.d.ts" />
import {Component, View, bootstrap,} from 'angular2/angular2';

class Names {}

// Annotation section
@Component({
    selector: 'my-app',
    injectables: [Names]
})
@View({
    template: '<h1>Hello {{ name }}</h1>'
})
// Component controller
class MyAppComponent {
    name: string;
    constructor(names: Names) {
        this.name = 'Alice';
    }
}

bootstrap(MyAppComponent);

附言如在 5 Min Quickstart ,我正在使用 TraceurSystemJSAngular2 alpha (23)

有人知道我错过了什么吗?

最佳答案

您的编译器不会向 MyAppComponent 添加参数属性(通过查看您的 pluker )。我认为这是问题所在。如果你添加

MyAppComponent.parameters = [[Names]]

那么一切都会好起来的。

  1. Here是你的修理工。
  2. Here在 TS 中是相同的例子(带 ES6)

UPD 感谢@GrayFox 指出正确的方法(见下面的评论):

For future references - use --emitDecoratorMetadata flag when using tsc or add emitDecoratorMetadata: true to the configuration if you're using gulp-typescript

参见 TypeScript 编译器选项 here (您可以在那里找到 emitDecoratorMetada)。

关于angular - 注入(inject)剂在 angular 2.0 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32970702/

相关文章:

angular - 订阅一个主题,第一次在 init 上不起作用

javascript - Visual Studio 2015 RTM - 更新 TypeScript 工具后失去 JavaScript 支持

Angular 2 登录到 Django Rest Framework 后端

angular - FCM getToken() 不返回任何内容

java - 保持 spring boot 和 Angular 应用程序之间的模型同步。备择方案?

visual-studio-2013 - 在 Visual Studio 2013 中使用 tsconfig.json? (网络项目)

generics - TypeScript类型参数实现多个接口(interface)

typescript - 模块 '"angular2/angular 2"' 没有导出成员 'For'

typescript - 使用装饰器获取已实现接口(interface)的列表

Angular 7 + ngx-bootstrap 3.1.3