订阅promise时收到错误

时间:2016-04-19 07:15:40

标签: angular rxjs5

当页面加载时我收到错误

people.ts?9320:22 SyntaxError: Unexpected token 
<(…)PeopleComponent.apiService.people.subscribe.error @ people.ts?
9320:22SafeSubscriber.__tryOrUnsub @ VM38600:166SafeSubscriber.error @ 
VM38600:127Subscriber._error @ VM38600:77Subscriber.error @ 
VM38600:57MapSubscriber._next @ VM38955:51Subscriber.next @ 
VM38600:51onLoad @ VM38808:57ZoneDelegate.invokeTask @ 
VM38526:423NgZoneImpl.inner.inner.fork.onInvokeTask @ 
VM38628:36ZoneDelegate.invokeTask @ VM38526:422Zone.runTask @ 
VM38526:320ZoneTask.invoke @ VM38526:490

这是我的代码:

export class PeopleComponent {
    people: any;
    constructor(apiService: Api) {
        apiService.people
            .subscribe({
                next: x => (this.people = x),
                error: e => console.error(e), //error line 22 is here!
                complete: () => console.log('Completed!')
            });
    }
}

我的Api服务:

import {Injectable} from 'angular2/core';
import { Http } from 'angular2/http';

@Injectable()
export class Api {
  people: any;

  constructor(http: Http) {
    this.people = http.get(require('./people.json'))
      .map(response => response.json());
  }
}

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

您可以尝试以这种方式订阅:

apiService.people
      .subscribe(
        x => {this.people = x;},
        e => {console.error(e);},
        () => {console.log('Completed!');}
      });

请参阅此文档: