gpt4 book ai didi

angular - Angular2 中的注入(inject)器

转载 作者:太空狗 更新时间:2023-10-29 17:02:37 24 4
gpt4 key购买 nike

我正在玩 Angular2。作为基础,我使用了 quickstart来自 angular.io 的项目页面。

一切似乎都工作正常,但是当我尝试将服务 (ItemService) 注入(inject)到我的 AppComponent 时,我得到以下异常:

Error during instantiation of Token(ComponentRef)!. ORIGINAL ERROR: Cannot resolve all parameters for AppComponent. Make sure they all have valid type or annotations.

我在互联网上看到过类似的问题(包括 stackoverflow,例如 this post ),但似乎都没有解决我的问题。有没有人知道问题可能是什么?

我还看到了一些解决方案(例如 Angular2 存储库中的解决方案),它们使用 Injectable-annotation 装饰可注入(inject)类。然而这对我不起作用,因为它没有在 angular.d.ts 中定义。我使用的版本有误吗?

您可以在以下 Plunker 中找到我的解决方案: http://plnkr.co/edit/7kK1BtcuEHjaspwLTmsg

作为记录,您还可以在下面找到我的两个文件。请注意,Plunker 中的 app.js 是从我下面的 TypeScript 文件生成的 JavaScript 文件,异常始终相同。

index.html:

<html>
<head>
<title>Testing Angular2</title>
<script src="https://github.jspm.io/jmcriffey/bower-traceur-runtime@0.0.87/traceur-runtime.js"></script>
<script src="https://jspm.io/system@0.16.js"></script>
<script src="https://code.angularjs.org/2.0.0-alpha.23/angular2.dev.js"></script>
</head>
<body>
<app></app>
<script>
System.import('js/app');
</script>
</body>
</html>

js/app.ts:

/// <reference path="../../typings/angular2/angular2.d.ts" />

import {Component, View, bootstrap, For, If} from "angular2/angular2";

class Item {
id:number;
name:string;

constructor(id:number, name:string) {
this.id = id;
this.name = name;
}
}

class ItemService {
getItems() {
return [
new Item(1, "Bill"),
new Item(2, "Bob"),
new Item(3, "Fred")
];
}
}

@Component({
selector: 'app',
injectables: [ItemService]
})
@View({
template: `<h1>Testing Angular2</h1>
<ul>
<li *for="#item of items">
{{item.id}}: {{item.name}} |
<a href="javascript:void(0);" (click)="toggleSelected(item);">
{{selectedItem == item ? "unselect" : "select"}}
</a>
</li>
</ul>
<item-details *if="selectedItem" [item]="selectedItem"></item-details>`,
directives: [For, If, DetailComponent]
})
class AppComponent {
items:Item[];
selectedItem:Item;

constructor(itemService:ItemService) {
this.items = itemService.getItems();
}

toggleSelected(item) {
this.selectedItem = this.selectedItem == item ? null : item;
}
}

@Component({
selector: 'item-details',
properties: {
item: "item"
}
})
@View({
template: `<span>You selected {{item.name}}</span>`
})
class DetailComponent {
item:Item;
}

bootstrap(AppComponent);

提前感谢您的任何想法!

最佳答案

检查几件事:-

1) 确保您使用的是从您的软件包位置安装的正确版本的 typescript (1.5.x)。 () 如果您安装了以前版本的 typescript ,那么仅使用“tsc”命令可能会指向环境路径中的现有 (< 1.5) 位置。

2) 确保在 tsc 命令中使用 --emitDecoratorMetadata 标志。这是必要的,以便 javascript 输出为装饰器创建元数据。

3) 错误 - 无法读取未定义的属性“注释” 因为 AppComponent 指令对尚未定义的 DetailComponent 有依赖性(到同步 __decorate( 函数运行以解决依赖性时)并且 TS 编译提升变量 DetailComponent 的方式是未定义的(下面的 IIFE 尚未运行)。尝试将 DetailComponent 的声明移动到 AppComponent< 之前。或者只是将它移动到不同的文件并导入它。

@Component({
selector: 'item-details',
properties: {
item: "item"
}
})
@View({
template: `<span>You selected {{item.name}}</span>`
})
class DetailComponent {
item:Item;
}

@Component({
selector: 'app',
injectables: [ItemService]
})
@View({
template: `<h1>Testing Angular2</h1>
<ul>
<li *for="#item of items">
{{item.id}}: {{item.name}} |
<a href="#" (click)="toggleSelected(item);">
{{selectedItem == item ? "unselect" : "select"}}
</a>
</li>
</ul>
<item-details *if="selectedItem" [item]="selectedItem"></item-details>`,
directives: [For, If, DetailComponent]
})

class AppComponent {
items:Item[];
selectedItem:Item;

constructor(itemService:ItemService) {
this.items = itemService.getItems();
}

toggleSelected(item) {
this.selectedItem = this.selectedItem == item ? null : item;
return false;
}
}

bootstrap(AppComponent);

编辑

从 angular a26 开始,如果您在遵循当前文档方面遇到问题(截至目前)follow this link if it helps因为有一些相关的变化。

关于angular - Angular2 中的注入(inject)器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30311514/

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