我正在努力在 angular2 中导入一个简单的服务。请帮我解决问题。我知道我错过了一些非常简单但找不到的东西。提前致谢
http://plnkr.co/edit/Yrh9gC8BMZdtUwy947Za?p=preview
//our root app component
import {Component, OnInit} from 'angular2/core'
import {DataService} from './app.service'
@Component({
selector: 'my-app',
providers: [DataService],
template: `
<div>
<h2>Hello {{name}}</h2>
</div>
`,
})
export class App implements OnInit{
constructor(service: DataService) {
this.name = 'Angular2'
}
ngOnInit(){
console.log(this.service) // prints Undefined here
this.name = this.service.setName('test');
}
}