Angular 2 Observable 订阅不工作
Angular 2 Observable subscribe not working
我正在尝试订阅组件初始化时的可观察对象,如下所示:
this.ticketService.getTicketsAsync(filters).subscribe(
tickets => this.ticketResponse = tickets,
() => console.log('hi'))
为什么第一个 lambda 表达式有效,而第二个 lambda 表达式却不能?
编辑:
这是 getTicketAsync 返回的代码:
getTicketsAsync(ticketFilters : TicketSearch): Observable<TicketResponse> {
let api_endpoint = AppSettings.API_ENDPOINT + 'WebReport/request_report'
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({headers : headers, withCredentials : true});
return this.http.post(api_endpoint, ticketFilters, options).map(this.extractData).catch(this.handleError);
}
第二个是catch
when observable throw error.
subscription = source.subscribe(
x => console.log('onNext: %s', x),
e => console.log('onError: %s', e),
() => console.log('onCompleted'));
https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/subscribe.md
解法:
1.使用onNext
钩子:
this.ticketService.getTicketsAsync(filters).subscribe(
tickets => {
this.ticketResponse = tickets;
console.log('hi');
},
() => console.log('ERROR!'));
2。使用 onCompleted
挂钩:
this.ticketService.getTicketsAsync(filters).subscribe(
tickets => this.ticketResponse = tickets,
error => console.log('ERROR: ' +error),
() => console.log('hi')
);
大箭头代表使您的代码像这样的函数
this.ticketService.getTicketsAsync(filters)
.subscribe(
function(tickets){
this.ticketResponse = tickets,
function(){
console.log("hi")
}
}
)
您正在传递两个参数(这是两个回调函数)。
我正在尝试订阅组件初始化时的可观察对象,如下所示:
this.ticketService.getTicketsAsync(filters).subscribe(
tickets => this.ticketResponse = tickets,
() => console.log('hi'))
为什么第一个 lambda 表达式有效,而第二个 lambda 表达式却不能?
编辑:
这是 getTicketAsync 返回的代码:
getTicketsAsync(ticketFilters : TicketSearch): Observable<TicketResponse> {
let api_endpoint = AppSettings.API_ENDPOINT + 'WebReport/request_report'
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({headers : headers, withCredentials : true});
return this.http.post(api_endpoint, ticketFilters, options).map(this.extractData).catch(this.handleError);
}
第二个是catch
when observable throw error.
subscription = source.subscribe(
x => console.log('onNext: %s', x),
e => console.log('onError: %s', e),
() => console.log('onCompleted'));
https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/subscribe.md
解法:
1.使用onNext
钩子:
this.ticketService.getTicketsAsync(filters).subscribe(
tickets => {
this.ticketResponse = tickets;
console.log('hi');
},
() => console.log('ERROR!'));
2。使用 onCompleted
挂钩:
this.ticketService.getTicketsAsync(filters).subscribe(
tickets => this.ticketResponse = tickets,
error => console.log('ERROR: ' +error),
() => console.log('hi')
);
大箭头代表使您的代码像这样的函数
this.ticketService.getTicketsAsync(filters)
.subscribe(
function(tickets){
this.ticketResponse = tickets,
function(){
console.log("hi")
}
}
)
您正在传递两个参数(这是两个回调函数)。