gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 18:02:34 25 4
gpt4 key购买 nike

我最近开始玩 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com