我有以下angular2应用程序注入了一个简单的依赖项,它不起作用。我错过了什么?
这是错误:
EXCEPTION: Cannot resolve all parameters for 'AppComponent'(?). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'AppComponent' is decorated with Injectable.
和代码:
import {Component} from 'angular2/core';
import {bootstrap} from 'angular2/platform/browser';
class DataService {
items: Array<any>;
constructor() {
this.items = [
{ name: 'Christoph Burgdorf' },
{ name: 'Pascal Precht' },
{ name: 'thoughtram' }
];
}
getItems() {
return this.items;
}
}
@Component({
selector: 'app',
providers: [DataService],
template: `
<ul>
<li *ngFor="#item of items">{{item.name}}</li>
</ul>
`
})
class AppComponent {
items: Array<any>;
constructor(dataService: DataService) {
this.items = dataService.getItems();
}
}
bootstrap(AppComponent, []);
答案 0 :(得分:1)
无法复制。我将您的代码添加到Plunker
https://plnkr.co/edit/0DTjG5?p=preview
无论有没有@Injectable()
,它似乎都能正常工作。
建议始终将@Injectable()
添加到服务中,但只有在服务具有构造函数参数时才需要它。
.