本文介绍了角2号提供程序错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建简单的入门应用具有角2玩,我试图做一个待办事项服务并注入他到我的组成部分,我得到这个错误:
I am creating simple starter app to play with angular 2 and i am trying to make a todo service and inject him to my component and i get this error:
无提供商TodoService! (TodoList的 - > TodoService)的
No provider for TodoService! (TodoList -> TodoService)
TodoService.ts
TodoService.ts
export class TodoService {
todos: Array<Object>
constructor() {
this.todos = [];
}
}
app.ts
app.ts
/// <reference path="typings/angular2/angular2.d.ts" />
import {Component, View, bootstrap, For, If} from 'angular2/angular2';
import {TodoService} from './TodoService'
@Component({
selector: 'my-app'
})
@View({
templateUrl: 'app.html',
directives: [For, If],
injectables: [TodoService]
})
class TodoList {
todos: Array<Object>
constructor(t: TodoService) {
this.todos = t.todos
}
addTodo(todo) {
this.todos.push({
done:false,
todo: todo.value
});
}
}
bootstrap(TodoList);
这是什么问题?
推荐答案
该注射剂是在指定的 @Component
不在 @View
The injectables are specified on the @Component
not on the @View
您有:
@Component({
selector: 'my-app'
})
@View({
templateUrl: 'app.html',
directives: [For, If],
injectables: [TodoService] // moving this line
})
将其更改为:
@Component({
selector: 'my-app',
injectables: [TodoService] // to here
})
@View({
templateUrl: 'app.html',
directives: [For, If]
})
这将使DI把它捡起来,并注入到你的组件。
This will allow DI to pick it up and inject it into your component.
这篇关于角2号提供程序错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!