我正在尝试从组件中的服务类访问一个简单的方法。
//myfriend.service.ts
export class MyFriendService(){
constructor(){}
testMe() {
window.alert("im friend service");
}
}
//myfriend.component.ts
import {Component} from 'angular2/core';
import {MyFriendService} from 'app/components/myfriend/myfriend.service';
@Component({
directives: [],
providers: [MyFriendService],
templateUrl: `app/components/myfriend/myfriend-view.html`
})
export class MyFriendCmp {
constructor(myFriendService: MyFriendService) {
myFriendService.testMe();
}
}
它抛出一个错误
myFriendService.testMe is not a function.
我花了很多时间来完成这项工作。但仍在尝试。帮助我解决这个问题。谢谢!.
最佳答案
哦,你能相信吗?我花了很长时间才弄清楚。但你已经把
不需要括号看看这一行 导出类 MyFriendService() <=此括号不是必需的。
我已经做了两到三个 plunkr,每次它工作时都花了很多时间来弄清楚你在括号方面出了问题。
export class MyFriendService{
constructor(){}
testMe() {
window.alert("im friend service");
}
}
关于dependency-injection - Angular2依赖注入(inject)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34838949/