加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

注射器不工作于角度2.0

发布时间:2020-12-17 07:43:58 所属栏目:安全 来源:网络整理
导读:我最近开始玩Angular2.我一直试图让注射器工作大约半天,但我仍然无法弄清楚我做错了什么. 为了尽可能简单,我在官方网页上复制了5 Min Quickstart的代码.演示本身工作正常,但是当我尝试使用注射器时,我会发现错误 ORIGINAL ERROR: Cannot resolve all paramet
我最近开始玩Angular2.我一直试图让注射器工作大约半天,但我仍然无法弄清楚我做错了什么.

为了尽可能简单,我在官方网页上复制了5 Min Quickstart的代码.演示本身工作正常,但是当我尝试使用注射器时,我会发现错误

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那样,我使用Traceur,SystemJS和Angular2 alpha(23)

有人知道我失踪了吗?

您的编译器不会向MyAppComponent添加参数属性(从查看您的 pluker).我认为这是问题.如果你添加
MyAppComponent.parameters = [[Names]]

那么一切都会奏效.

> Here是你的解决方案.
> 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).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读