AngularJS 2 - 服务错误

时间:2016-04-05 17:21:16

标签: angular

我对我的简单服务做错了,但我无法理解。

app.component.ts

import { MeetingService } from '../../shared/services/meeting.service';

@Component({
  viewProviders: [MeetingService],
})

meetingService.ts

@Injectable();

export class MeetingService {

  constructor(private http: Http) {}

  getList() {
    return this.http.get('../fake-data/meetingList.json')
      .map(response => response.json());
  }
}

meetingList.component.ts

import { MeetingService } from '../../shared/services/meeting.service';

@Component({
  viewProviders: [HTTP_PROVIDERS, MeetingService],
})

export class MeetingListComponent {
  private meetingList: any;

  constructor(private meetingService: MeetingService) {}

  getList() {
    this.meetingService.getList().subscribe(
      data => { this.meetingList = data; },
      err  => console.error(err),
      ()   => console.log('done loading meeting list')
    );
  }

  ngOnInit() {
    this.getList();
  }
}

错误

EXCEPTION: Cannot resolve all parameters for 'MeetingService'(?). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'MeetingService' is decorated with Injectable.

有人可以看一下吗?谢谢。

2 个答案:

答案 0 :(得分:2)

;

之后删除@Injectable()
@Injectable()
export class MeetingService {

  constructor(private http: Http) {}

  getList() {
    return this.http.get('../fake-data/meetingList.json')
      .map(response => response.json());
  }
}

答案 1 :(得分:0)

您没有显示引导代码 - 您是否在引导期间向应用程序提供商添加了Http,例如

bootstrap(App, [HTTP_PROVIDERS])