如何控制在订阅中记录结果?

时间:2017-04-26 08:14:12

标签: angular typescript console

在我的角度应用程序中,我正在调用服务并获取数据,我需要在获取结果后添加控制台日志。我在哪里可以添加控制台日志?

 this.searchTerm.asObservable()
            .debounceTime(300)      
            .distinctUntilChanged() 
            .switchMap(term => this.doFind(term))
            .subscribe(data => this.results = data
            , error => {
                this.errorMessage = error;
            });

3 个答案:

答案 0 :(得分:7)

使用代码块

this.searchTerm.asObservable()
        .debounceTime(300)      
        .distinctUntilChanged() 
        .switchMap(term => this.doFind(term))
        .subscribe(data => {
           this.results = data;
           console.log('data', data);
        })
        , error => {
            this.errorMessage = error;
        });

do运算符

 this.searchTerm.asObservable()
        .debounceTime(300)      
        .distinctUntilChanged() 
        .switchMap(term => this.doFind(term))
        .do(val => condole.log(val))
        .subscribe(data => this.results = data
        , error => {
            this.errorMessage = error;
        });

答案 1 :(得分:3)

您正在使用箭头功能的简写版本,请使用更大的版本:)

.subscribe((data) => {
     this.results = data;
     console.log(data);
 }, error => {
    this.errorMessage = error;
 });

答案 2 :(得分:1)

你可以这样做吗

  this.searchTerm.asObservable()
        .debounceTime(300)      
        .distinctUntilChanged() 
        .switchMap(term => this.doFind(term))
        .subscribe(
            data=>this.message=data.msg,
            error=>alert('errer'),
            ()=>{console.log('finished');}

            );